chore: view primary key (#3107)

This commit is contained in:
ChengLei Shao 2023-11-28 19:45:38 +08:00 committed by GitHub
parent 9ed6993130
commit d2d885b2a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 1 deletions

View File

@ -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: {

View File

@ -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;