chore: view primary key (#3107)
This commit is contained in:
parent
9ed6993130
commit
d2d885b2a6
@ -1,4 +1,4 @@
|
||||
import Database, { Field, Repository, ViewCollection, ViewFieldInference, DataTypes } from '@nocobase/database';
|
||||
import Database, { Repository, ViewCollection, ViewFieldInference } from '@nocobase/database';
|
||||
import Application from '@nocobase/server';
|
||||
import { uid } from '@nocobase/utils';
|
||||
import { createApp } from '../index';
|
||||
@ -28,6 +28,65 @@ describe('view collection', function () {
|
||||
await app.destroy();
|
||||
});
|
||||
|
||||
it('should use id field as only primary key', async () => {
|
||||
await collectionRepository.create({
|
||||
values: {
|
||||
name: 'groups',
|
||||
fields: [{ name: 'name', type: 'string' }],
|
||||
},
|
||||
context: {},
|
||||
});
|
||||
|
||||
await collectionRepository.create({
|
||||
values: {
|
||||
name: 'users',
|
||||
fields: [
|
||||
{ name: 'name', type: 'string' },
|
||||
{ type: 'belongsTo', name: 'group', foreignKey: 'group_id' },
|
||||
],
|
||||
},
|
||||
context: {},
|
||||
});
|
||||
|
||||
const User = db.getCollection('users');
|
||||
|
||||
const assoc = User.model.associations.group;
|
||||
const foreignKey = assoc.foreignKey;
|
||||
const foreignField = User.model.rawAttributes[foreignKey].field;
|
||||
|
||||
const viewName = `test_view_${uid(6)}`;
|
||||
await db.sequelize.query(`DROP VIEW IF EXISTS ${viewName}`);
|
||||
|
||||
const createSQL = `CREATE VIEW ${viewName} AS SELECT id, ${foreignField}, name FROM ${db
|
||||
.getCollection('users')
|
||||
.quotedTableName()}`;
|
||||
|
||||
await db.sequelize.query(createSQL);
|
||||
|
||||
const inferredFields = await ViewFieldInference.inferFields({
|
||||
db,
|
||||
viewName,
|
||||
viewSchema: 'public',
|
||||
});
|
||||
|
||||
await collectionRepository.create({
|
||||
values: {
|
||||
name: viewName,
|
||||
view: true,
|
||||
fields: [
|
||||
{ name: 'id', type: 'bigInt' },
|
||||
{ name: 'group_id', type: 'bigInt', primaryKey: true },
|
||||
{ name: 'name', type: 'string' },
|
||||
],
|
||||
schema: db.inDialect('postgres') ? 'public' : undefined,
|
||||
},
|
||||
context: {},
|
||||
});
|
||||
|
||||
const viewCollection = db.getCollection(viewName);
|
||||
expect(viewCollection.model.primaryKeyAttributes).toEqual(['id']);
|
||||
});
|
||||
|
||||
it('should create view collection with belongs to association', async () => {
|
||||
await collectionRepository.create({
|
||||
values: {
|
||||
|
@ -78,6 +78,18 @@ export class CollectionModel extends MagicAttributeModel {
|
||||
fields = fields.filter((field) => options.includeFields.includes(field.name));
|
||||
}
|
||||
|
||||
if (this.options.view && fields.find((f) => f.name == 'id')) {
|
||||
// set id field to primary key, other primary key to false
|
||||
fields = fields.map((field) => {
|
||||
if (field.name == 'id') {
|
||||
field.set('primaryKey', true);
|
||||
} else {
|
||||
field.set('primaryKey', false);
|
||||
}
|
||||
return field;
|
||||
});
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
const instances: FieldModel[] = fields;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user