fix: filterTargetKey

This commit is contained in:
chenos 2024-02-07 10:23:28 +08:00
parent 60aa6babf5
commit 5b2f38da14
2 changed files with 17 additions and 6 deletions

View File

@ -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() {

View File

@ -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 (<any>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');