typeorm: if OneToMany entity was updated right before, em.save() does not save entity attached in ManyToOne, resulting in parentId column
It’s very strange. I assume I did nothing wrong.
Here is column:
@ManyToOne(type => RoastAttempt, ra => ra.reactions)
attempt: RoastAttempt;
here is I’m saving:
const reaction = new RoastAttemptReaction();
reaction.attempt = a;
reaction.type = type;
reaction.user = u;
await this.entityManager.save(reaction);
I did console.log of reaction before saving - entity is there. in the attempt field.
I should also mention .user field on the other hand is populated and saved.
version: “typeorm”: “^0.1.7”
edit: em.create() has the same behaviour
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 24 (24 by maintainers)
Commits related to this issue
- fixes #1319 — committed to typeorm/typeorm by deleted user 7 years ago
- fixed persistence flag bug in relation metadata, more tests for #1319 — committed to typeorm/typeorm by deleted user 7 years ago
Thank you for your feedback @mrkmg . I decided not to do
doctorsince it still will bring user issues because most of them will not ever know about “doctor” command and they will keep throwing issues.Regarding to warning in console - it will look very weird in my opinion, better to throw an error instead I think. This change will be available in the next
nextversion.You have this error because you have initialized array:
Remove
= []and you’ll not have this error. Avoid array initialization in your relations, read here for more info.When typeorm tries to save your
parentit sees its children array is empty and thinks you removed all childrens from parent and detaches them.Hmmmm… I think a
typeorm doctorcommand would be cool, but if the only if there is more than one check. From this conversation, all I am seeing is the desire to warn about initializers on relations. Seeing as that’s the only case, I think a console.warn is good enough.If we do emit warnings, we should also provide some sort of “suppressWarnings” flag that allows developers to turn off warnings.
I was thinking, how cool would it be to have tslint, or even typescript give a compile-time warning about it, but after looking through the docs on how types work, I can not see a way to accomplish that, so a runtime warning is the only option I see.
If we can come up with a few checks for best practice, then I can see the use-case of a full command like
doctor. Some ideas could be:That’s my two cents.
value [] means everything was removed, just think how it would work if you wanted to really remove all children from your parent.
I have no idea how to warn about this thing.
yes this is tricky, unfortunately
Yes, I’m using VSCode, I had no problem with intellisense and performance but I noticed that sometimes the whole TypeScript language service hangs and don’t check the code nor underline the error, I need to restart it or wait a moment 😕
np, will try to do that