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
in bulkCreate, updateOnDuplicate doesn’t work on included table #14642
Comments
@John6Connor I'll take this one. May ask you for some clarifications as I move forward. |
@ephys Quick question as I move into this - I notice this seems to be using |
The fact that it is written in sequelize-typescript shouldn't impact this, but I think the first step would be writing a short test that tries to replicate the issue without sequelize-typescript |
This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂 |
@ephys Seems like I've found the problem, but I'm not sure how we want to solve it. In The code in question can be found here. As a side note, how would you feel about a refactor of |
@John6Connor Just to clarify, are you expecting that the |
If I understand the issue correctly, I think we can classify this as a missing feature rather than a bug In terms of API design, I don't think we should copy the I think we have multiple options (I've trimmed the example a bit): Design 1
await TeamModel.bulkCreate([{
code: 'team001',
trophies:1,
players: [{
code: 'player0001',
name: 'playername0001',
}],
}], {
// this applies to the TeamModel bulk insert query
updateOnDuplicate: ['trophies'],
include: [{
association: 'players',
// this applies to the TeamModel.players bulk insert query
updateOnDuplicate: ['name'],
}],
}); Design 2Only one await TeamModel.bulkCreate([{
code: 'team001',
trophies:1,
players: [{
code: 'player0001',
name: 'playername0001',
}],
}], {
updateOnDuplicate: [
// this applies to the TeamModel bulk insert query
'trophies',
// this applies to the TeamModel.players bulk insert query
'players.name',
],
include: [{
association: 'players',
}],
}); |
@ephys Personally, I prefer design 1. I think it's cleaner to have It also allows for some future-proofing where we could create a general "override" mechanism in case something like this occurs with something other than |
I also think design 1 is better |
Hello! Following up on this. Seems the PR is already created but not yet merged so I just wanted to ping |
Voting/checking for this as well. Please advise. |
The relevant PR is #14739, it's pending a review |
https://github.com/sequelize/sequelize/pull/14739 not yet merged |
Issue Creation Checklist
Bug Description
When I execute the method bulkCreate with the options updateOnDuplicate and include, the updateOnDuplicate option is applied only in the main table and not in the included one. This imply that if I run two times bulkCreate with the same arguments it produce an error because, in the included table, it tries to insert records that already exist instead to try to update them.
Reproducible Example
I defined two models Teams and Players with the relation one-to-many.
Team model:
Player model:
When I try to execute the following code, at the second time it produce an error
the error is:
parent: Error: Duplicate entry 'player0001' for key 'PlayerModels.PRIMARY'
By looking the queries that Sequelize produces, we can see that it puts the string
ON DUPLICATE KEY UPDATE ...
only in the INSERT query of the main table and not in the one that is included:What do you expect to happen?
I expect that also the included table receive the option updateOnDuplicate and therefore the ON DUPLICATE KEY UPDATE ...` is applied as well in the included query
What is actually happening?
Sequelize tries to insert records that are already present in included table instead to try to update them
Environment
Would you be willing to resolve this issue by submitting a Pull Request?
Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.
The text was updated successfully, but these errors were encountered: