From 78402c394ba52bc90ad838b3c699e906b1749706 Mon Sep 17 00:00:00 2001 From: chareice Date: Thu, 24 Nov 2022 18:03:10 +0800 Subject: [PATCH] fix: association not found error --- .../core/database/src/fields/belongs-to-field.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/core/database/src/fields/belongs-to-field.ts b/packages/core/database/src/fields/belongs-to-field.ts index a216fdb84..dababfece 100644 --- a/packages/core/database/src/fields/belongs-to-field.ts +++ b/packages/core/database/src/fields/belongs-to-field.ts @@ -84,18 +84,21 @@ export class BelongsToField extends RelationField { const foreignKey = this.options.foreignKey; const field1 = collection.getField(foreignKey); - const field2 = tcoll ? tcoll.findField((field) => { - return field.type === 'hasMany' && field.foreignKey === foreignKey; - }) : null; + const field2 = tcoll + ? tcoll.findField((field) => { + return field.type === 'hasMany' && field.foreignKey === foreignKey; + }) + : null; if (!field1 && !field2) { collection.model.removeAttribute(foreignKey); } const association = collection.model.associations[this.name]; - const reference = this.reference(association); - - this.database.referenceMap.removeReference(reference); + if (association) { + const reference = this.reference(association); + this.database.referenceMap.removeReference(reference); + } this.clearAccessors(); // 删掉 model 的关联字段