fix: unbind error (#1094)

This commit is contained in:
ChengLei Shao 2022-11-16 17:36:42 +08:00 committed by GitHub
parent ff267456de
commit f8f9b8cc9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -154,19 +154,27 @@ export class HasOneField extends RelationField {
database.removePendingField(this); database.removePendingField(this);
// 如果关系表内没有显式的创建外键字段,删除关系时,外键也删除掉 // 如果关系表内没有显式的创建外键字段,删除关系时,外键也删除掉
const tcoll = database.collections.get(this.target); const tcoll = database.collections.get(this.target);
if (tcoll) {
const foreignKey = this.options.foreignKey; const foreignKey = this.options.foreignKey;
const field = tcoll.findField((field) => { const field = tcoll.findField((field) => {
if (field.name === foreignKey) { if (field.name === foreignKey) {
return true; return true;
} }
return field.type === 'belongsTo' && field.foreignKey === foreignKey; return field.type === 'belongsTo' && field.foreignKey === foreignKey;
}); });
if (!field) { if (!field) {
tcoll.model.removeAttribute(foreignKey); tcoll.model.removeAttribute(foreignKey);
} }
}
const association = collection.model.associations[this.name]; const association = collection.model.associations[this.name];
if (association) {
this.database.referenceMap.removeReference(this.reference(association)); this.database.referenceMap.removeReference(this.reference(association));
}
this.clearAccessors(); this.clearAccessors();
// 删掉 model 的关联字段 // 删掉 model 的关联字段