From f8f9b8cc9f1eed2cde855e0c338b7825d64ef6c9 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Wed, 16 Nov 2022 17:36:42 +0800 Subject: [PATCH] fix: unbind error (#1094) --- .../core/database/src/fields/has-one-field.ts | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/core/database/src/fields/has-one-field.ts b/packages/core/database/src/fields/has-one-field.ts index 2c1734311..da2be12b5 100644 --- a/packages/core/database/src/fields/has-one-field.ts +++ b/packages/core/database/src/fields/has-one-field.ts @@ -154,19 +154,27 @@ export class HasOneField extends RelationField { database.removePendingField(this); // 如果关系表内没有显式的创建外键字段,删除关系时,外键也删除掉 const tcoll = database.collections.get(this.target); - const foreignKey = this.options.foreignKey; - const field = tcoll.findField((field) => { - if (field.name === foreignKey) { - return true; + + if (tcoll) { + const foreignKey = this.options.foreignKey; + + const field = tcoll.findField((field) => { + if (field.name === foreignKey) { + return true; + } + return field.type === 'belongsTo' && field.foreignKey === foreignKey; + }); + + if (!field) { + tcoll.model.removeAttribute(foreignKey); } - return field.type === 'belongsTo' && field.foreignKey === foreignKey; - }); - if (!field) { - tcoll.model.removeAttribute(foreignKey); } const association = collection.model.associations[this.name]; - this.database.referenceMap.removeReference(this.reference(association)); + + if (association) { + this.database.referenceMap.removeReference(this.reference(association)); + } this.clearAccessors(); // 删掉 model 的关联字段