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);
const foreignKey = this.options.foreignKey;
const field = tcoll.findField((field) => { if (tcoll) {
if (field.name === foreignKey) { const foreignKey = this.options.foreignKey;
return true;
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]; 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(); this.clearAccessors();
// 删掉 model 的关联字段 // 删掉 model 的关联字段