fix: do not initialize FKs to null (#10337)

This commit is contained in:
Harminder Virk
2024-11-28 17:15:08 +05:30
committed by GitHub
parent f7279f1b96
commit d57c739052
5 changed files with 52 additions and 22 deletions
@@ -237,7 +237,30 @@ export function defineBelongsToRelationship(
return
}
this[foreignKeyName] ??= this[relationship.name]?.id ?? null
/**
* Do not override the existing foreign key value if
* exists
*/
if (this[foreignKeyName] !== undefined) {
return
}
/**
* Set the foreign key when the relationship is initialized
* as null
*/
if (this[relationship.name] === null) {
this[foreignKeyName] = null
return
}
/**
* Set the foreign key when the relationship is initialized
* and as the id
*/
if (this[relationship.name] && "id" in this[relationship.name]) {
this[foreignKeyName] = this[relationship.name].id
}
}
/**