From 1828e15bb99b094086929da1cc6380e58ed52b55 Mon Sep 17 00:00:00 2001 From: chenos Date: Sat, 9 Apr 2022 14:10:00 +0800 Subject: [PATCH] fix: skip if targetKey or sourceKey exists --- .../src/__tests__/fields/hasMany.test.ts | 4 +++- .../src/hooks/beforeInitOptions.ts | 22 ++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/packages/plugin-collection-manager/src/__tests__/fields/hasMany.test.ts b/packages/plugin-collection-manager/src/__tests__/fields/hasMany.test.ts index 734357e7f..661f2a570 100644 --- a/packages/plugin-collection-manager/src/__tests__/fields/hasMany.test.ts +++ b/packages/plugin-collection-manager/src/__tests__/fields/hasMany.test.ts @@ -1,4 +1,4 @@ -import Database, { Collection as DBCollection, StringFieldOptions } from '@nocobase/database'; +import Database, { Collection as DBCollection } from '@nocobase/database'; import Application from '@nocobase/server'; import { createApp } from '..'; @@ -38,6 +38,7 @@ describe('hasMany field options', () => { target: 'foos', }, }); + await field.reload(); const json = field.toJSON(); expect(json).toMatchObject({ type: 'hasMany', @@ -62,6 +63,7 @@ describe('hasMany field options', () => { targetKey: 'ghi', }, }); + await field.reload(); expect(field.toJSON()).toMatchObject({ name: 'foos', type: 'hasMany', diff --git a/packages/plugin-collection-manager/src/hooks/beforeInitOptions.ts b/packages/plugin-collection-manager/src/hooks/beforeInitOptions.ts index 6898af98d..38f0d800b 100644 --- a/packages/plugin-collection-manager/src/hooks/beforeInitOptions.ts +++ b/packages/plugin-collection-manager/src/hooks/beforeInitOptions.ts @@ -3,25 +3,35 @@ import { uid } from '@nocobase/utils'; import { Model } from 'sequelize'; const setTargetKey = (db: Database, model: Model) => { + if (model.get('targetKey')) { + return; + } const target = model.get('target') as any; if (db.hasCollection(target)) { const targetModel = db.getCollection(target).model; model.set('targetKey', targetModel.primaryKeyAttribute || 'id'); + } else { + model.set('targetKey', 'id'); } }; const setSourceKey = (db: Database, model: Model) => { + if (model.get('sourceKey')) { + return; + } const source = model.get('collectionName') as any; if (db.hasCollection(source)) { const sourceModel = db.getCollection(source).model; model.set('sourceKey', sourceModel.primaryKeyAttribute || 'id'); + } else { + model.set('sourceKey', 'id'); } }; export const beforeInitOptions = { belongsTo(model: Model, { database }) { const defaults = { - targetKey: 'id', + // targetKey: 'id', foreignKey: `f_${uid()}`, }; for (const key in defaults) { @@ -34,8 +44,8 @@ export const beforeInitOptions = { }, belongsToMany(model: Model, { database }) { const defaults = { - targetKey: 'id', - sourceKey: 'id', + // targetKey: 'id', + // sourceKey: 'id', through: `t_${uid()}`, foreignKey: `f_${uid()}`, otherKey: `f_${uid()}`, @@ -51,8 +61,8 @@ export const beforeInitOptions = { }, hasMany(model: Model, { database }) { const defaults = { - targetKey: 'id', - sourceKey: 'id', + // targetKey: 'id', + // sourceKey: 'id', foreignKey: `f_${uid()}`, target: `t_${uid()}`, }; @@ -67,7 +77,7 @@ export const beforeInitOptions = { }, hasOne(model: Model, { database }) { const defaults = { - sourceKey: 'id', + // sourceKey: 'id', foreignKey: `f_${uid()}`, }; for (const key in defaults) {