fix: skip if targetKey or sourceKey exists
This commit is contained in:
parent
c406e5eb8b
commit
1828e15bb9
@ -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',
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user