typeorm: Saving multiple entities with related objects at the same time does not work with cascade = true

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] sqlite [ ] sqljs [ ] websql

TypeORM version:

[ ] latest [x] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

Repository to reproduce the issue: https://github.com/darkbasic/typeorm-repro2

const chat1 = new Chat({
  allTimeMembers: [user1, user5],
  listingMembers: [user1, user5],
});
await connection.manager.save(chat1);

await connection.manager.save([
  new Message({
    chat: chat1,
    sender: user1,
    content: 'I should buy a boat',
    type: MessageType.TEXT,
    holders: [user1, user5],
    recipients: [
      new Recipient({
        user: user5,
      }),
    ],
  }),
  new Message({
    chat: chat1,
    sender: user1,
    content: 'You still there?',
    type: MessageType.TEXT,
    holders: [user1, user5],
    recipients: [
      new Recipient({
        user: user5,
      }),
    ],
  })
]);

If I save more entities at the same time (each one with realated objects and cascade set to true) it silently fails to insert them (it inserts only one of them).

The same exact code split into diffent calls works flawlessly:

const chat1 = new Chat({
  allTimeMembers: [user1, user5],
  listingMembers: [user1, user5],
});
await connection.manager.save(chat1);

await connection.manager.save(new Message({
  chat: chat1,
  sender: user1,
  content: 'I should buy a boat',
  type: MessageType.TEXT,
  holders: [user1, user5],
  recipients: [
    new Recipient({
      user: user5,
    }),
  ],
}));

await connection.manager.save(new Message({
  chat: chat1,
  sender: user1,
  content: 'You still there?',
  type: MessageType.TEXT,
  holders: [user1, user5],
  recipients: [
    new Recipient({
      user: user5,
    }),
  ],
}));

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 18 (18 by maintainers)

Commits related to this issue

Most upvoted comments

Its a bug and fix will be released in 0.2.0-alpha.16.

Some advances:

You have a data saved by cascades in which you have multiple primary keys which are referenced to another entities one of them is saved another is not, ahh soo complex. I strongly recommend you to keep things simple and avoid confusion for the ORM, yourself and people who is going to work with your code.

Also, be careful with such expression:

  this.recipients = recipients.map(recipient => ({...recipient, message: this}));

resulted object is instance of Object, not Recipient. This may lead to unexpected behaviour. Its much better to simply assign message property of recipient:

recipients.forEach(recipient => recipient.message = this);
this.recipients = recipients;

Also you have several other issues in your code like duplicate interface names and not exported interfaces. Thanks for the good repo, it would be great if you next time create a PR with a failing test like what I did here(5361167) with your code.

you don’t need to use update method. You load your entity with your relation, let’s say chat with members, push new member and send chat entity to save method.

I just tried it and it works flawlessly, thank you very much!