-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot set embedded entity to null #3913
Cannot set embedded entity to null #3913
Comments
I ran into a bug today that was caused by this. It's surprising that you cannot set an embedded object to null and have all columns be null. It seems to just be ignored. |
Seen this also. Really against my preference to collapse embedded entities to the top-level entity, but that's what I've had to do for now. |
As well as this, if all embedded columns are null on load, it's not possible to use the field in the top-level entity as a check. e.g. if I had an embedded This isn't exactly the same problem as this issue, but I feel it's in the same realm, where it would be nice to be able to read/write |
That is true and leads to a subtle bug in my case. Unfortunately, there is no Firstly we need a function that returns null when all object properties are null. export function asNullOnNullProperties<T>(obj: T): T | null {
for (const key in obj) {
if (obj[key] != null) {
return obj;
}
}
return null;
} Now we can use some listeners to modify the embedded entity value. It depends on your use case, but this should be enough. @AfterLoad()
@AfterInsert()
@AfterUpdate()
handleEmbeddedEntities() {
this.myEmbeddedEntity = asNullOnNullProperties(this.myEmbeddedEntity);
} Hope it helps 👍 |
Support nullable embedded fields such that embedded documents in MongoDB can correctly be returned as null for a subdocument that is explicitly nullable. Closes: typeorm#3913
Issue type:
[ ] question
[x] bug report
[ ] feature request
[ ] documentation issue
Database system/driver:
[ ]
cordova
[ ]
mongodb
[ ]
mssql
[ ]
mysql
/mariadb
[ ]
oracle
[x]
postgres
[ ]
cockroachdb
[ ]
sqlite
[ ]
sqljs
[ ]
react-native
[ ]
expo
TypeORM version:
[x]
latest
[ ]
@next
[ ]
0.x.x
(or put your version here)Steps to reproduce or a small repository showing the problem:
Check out this small repository: https://github.com/nomadoda/typeorm-embedded-bug
I have an embedded entity:
And I'm using it in this entity:
Consider this:
Expected outcome
Setting salary to null should set all embedded fields in database to null.
Actual outcome
Salary did not change on update
The text was updated successfully, but these errors were encountered: