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

Most upvoted comments

Thank you for your feedback @mrkmg . I decided not to do doctor since 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 next version.

You have this error because you have initialized array:

  @OneToMany(type => Child, c => c.parent)
  children: Child[] = [];

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 parent it sees its children array is empty and thinks you removed all childrens from parent and detaches them.

Hmmmm… I think a typeorm doctor command 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:

  • Missing inverse of relationship
  • Type MisMatch (saying “integer” db type, but declaring the property as type “string”)
  • Recursive “eager” checks

That’s my two cents.

so the value is []

value [] means everything was removed, just think how it would work if you wanted to really remove all children from your parent.

Is there way to warn about this stuff or something?

I have no idea how to warn about this thing.

this caveat.

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