diff --git a/packages/core/database/src/collection.ts b/packages/core/database/src/collection.ts index 54276a880a..77a27c2358 100644 --- a/packages/core/database/src/collection.ts +++ b/packages/core/database/src/collection.ts @@ -151,12 +151,14 @@ export class Collection< } get filterTargetKey() { - const targetKey = lodash.get(this.options, 'filterTargetKey', this.model.primaryKeyAttribute); - if (!targetKey && this.model.rawAttributes['id']) { - return 'id'; + const targetKey = this.options?.filterTargetKey; + if (targetKey && this.model.getAttributes()[targetKey]) { + return targetKey; } - - return targetKey; + if (this.model.primaryKeyAttributes.length > 1) { + return null; + } + return this.model.primaryKeyAttribute; } get name() { diff --git a/packages/plugins/@nocobase/plugin-collection-manager/src/server/models/collection.ts b/packages/plugins/@nocobase/plugin-collection-manager/src/server/models/collection.ts index 1716b20b5f..dedee6a5eb 100644 --- a/packages/plugins/@nocobase/plugin-collection-manager/src/server/models/collection.ts +++ b/packages/plugins/@nocobase/plugin-collection-manager/src/server/models/collection.ts @@ -1,7 +1,7 @@ import Database, { Collection, MagicAttributeModel, SyncOptions, Transactionable } from '@nocobase/database'; import lodash from 'lodash'; -import { FieldModel } from './field'; import { QueryInterfaceDropTableOptions } from 'sequelize'; +import { FieldModel } from './field'; interface LoadOptions extends Transactionable { // TODO @@ -15,6 +15,15 @@ export class CollectionModel extends MagicAttributeModel { return (this.constructor).database; } + toJSON() { + const json = super.toJSON(); + if (!json.filterTargetKey) { + const collection = this.db.getCollection(json.name); + json.filterTargetKey = collection?.filterTargetKey; + } + return json; + } + async load(loadOptions: LoadOptions = {}) { const { skipExist, skipField, resetFields, transaction } = loadOptions; const name = this.get('name');