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
- fixes #1551 — committed to typeorm/typeorm by deleted user 6 years ago
- fixes #1551, version bump — committed to typeorm/typeorm by deleted user 6 years ago
- skipped tests #1551 — committed to typeorm/typeorm by deleted user 6 years ago
- working "github issues > #1551 complex example of cascades + multiple primary keys = persistence order" — committed to typeorm/typeorm by bbakhrom 5 years ago
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:
resulted object is instance of Object, not Recipient. This may lead to unexpected behaviour. Its much better to simply assign message property of recipient:
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
updatemethod. You load your entity with your relation, let’s say chat with members, push new member and send chat entity tosavemethod.I just tried it and it works flawlessly, thank you very much!