diff --git a/packages/plugins/@nocobase/plugin-collection-manager/src/server/__tests__/fields/belongs-to-many.test.ts b/packages/plugins/@nocobase/plugin-collection-manager/src/server/__tests__/fields/belongs-to-many.test.ts index ead47cb27..c8cda8d5e 100644 --- a/packages/plugins/@nocobase/plugin-collection-manager/src/server/__tests__/fields/belongs-to-many.test.ts +++ b/packages/plugins/@nocobase/plugin-collection-manager/src/server/__tests__/fields/belongs-to-many.test.ts @@ -34,6 +34,44 @@ describe('belongsToMany', () => { await app.destroy(); }); + it('should throw error when through table foreign keys are same name', async () => { + await Collection.repository.create({ + values: { + name: 'A', + }, + context: {}, + }); + + await Collection.repository.create({ + values: { + name: 'B', + }, + context: {}, + }); + + let error; + + try { + await Field.repository.create({ + values: { + name: 'bs', + type: 'belongsToMany', + collectionName: 'A', + target: 'B', + sourceKey: 'id', + targetKey: 'id', + foreignKey: 'a_id', + otherKey: 'a_id', + }, + context: {}, + }); + } catch (e) { + error = e; + } + + expect(error).toBeDefined(); + }); + it('should define belongs to many when change alias name', async () => { await Collection.repository.create({ values: { diff --git a/packages/plugins/@nocobase/plugin-collection-manager/src/server/hooks/beforeCreateForValidateField.ts b/packages/plugins/@nocobase/plugin-collection-manager/src/server/hooks/beforeCreateForValidateField.ts new file mode 100644 index 000000000..b413fd2eb --- /dev/null +++ b/packages/plugins/@nocobase/plugin-collection-manager/src/server/hooks/beforeCreateForValidateField.ts @@ -0,0 +1,9 @@ +export function beforeCreateForValidateField() { + return async (model, { transaction }) => { + if (model.type === 'belongsToMany') { + if (model.get('foreignKey') === model.get('otherKey')) { + throw new Error('foreignKey and otherKey can not be the same'); + } + } + }; +} diff --git a/packages/plugins/@nocobase/plugin-collection-manager/src/server/server.ts b/packages/plugins/@nocobase/plugin-collection-manager/src/server/server.ts index 45251459a..ddb49f512 100644 --- a/packages/plugins/@nocobase/plugin-collection-manager/src/server/server.ts +++ b/packages/plugins/@nocobase/plugin-collection-manager/src/server/server.ts @@ -18,6 +18,7 @@ import { CollectionModel, FieldModel } from './models'; import collectionActions from './resourcers/collections'; import viewResourcer from './resourcers/views'; import sqlResourcer from './resourcers/sql'; +import { beforeCreateForValidateField } from './hooks/beforeCreateForValidateField'; export class CollectionManagerPlugin extends Plugin { public schema: string; @@ -110,6 +111,8 @@ export class CollectionManagerPlugin extends Plugin { } }); + this.app.db.on('fields.beforeCreate', beforeCreateForValidateField()); + this.app.db.on('fields.afterCreate', afterCreateForReverseField(this.app.db)); this.app.db.on('fields.afterCreate', async (model: FieldModel, { context, transaction }) => {