-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
fix(model): updateOnDuplicate handles composite keys now #11984
Conversation
Codecov Report
@@ Coverage Diff @@
## master #11984 +/- ##
==========================================
+ Coverage 90.11% 96.23% +6.12%
==========================================
Files 93 95 +2
Lines 9106 9202 +96
==========================================
+ Hits 8206 8856 +650
+ Misses 900 346 -554
Continue to review full report at Codecov.
|
Will this fix be released under v5? |
@cxcorp I am not sure if it will get fixed in v5 as well but there are very little breaking changes in v6 (yet) so the migration from v5 to v6 should be nearly seamless. |
Is this fix released already? I wanted to bulkCreate something with a composite primary key, but it keeps failing me. I am using postgres. Have tried both v5 and v6 of sequelize. If not (going to be soon) released. What is the workaround? |
@ericjansen1988 no, not yet it is just on master right now. There are two possibilities. But those possibilities should just be done if you know what you are doing and which effects it could have for the future:
$ npm i https://github.com/sequelize/sequelize But I just recommend this if you are aware of the risks pulling from master directly (use a package-lock too just in case). This version is still in Beta, there might be other bugs appearing - which I don't think so, but the risk is still there. Remember, if you might update to stable v6 the official API could be changed.
This could be leading to merging conflicts, depending on the codebase. |
@@ -2708,7 +2708,7 @@ class Model { | |||
// Get primary keys for postgres to enable updateOnDuplicate | |||
options.upsertKeys = _.chain(model.primaryKeys).values().map('field').value(); | |||
if (Object.keys(model.uniqueKeys).length > 0) { | |||
options.upsertKeys = _.chain(model.uniqueKeys).values().filter(c => c.fields.length === 1).map(c => c.fields[0]).value(); | |||
options.upsertKeys = _.chain(model.uniqueKeys).values().filter(c => c.fields.length >= 1).map(c => c.fields).reduce(c => c[0]).value(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused about the reduce
here, would this not effectively end up returning undefined
all the time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you are correct. The initializer for the reduce is undefined
(not specified), and c
is the previous
argument, so it would effectively always return the undefined, leaving the actual values untouched.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to the .reduce
updateOnDuplicate does not work for Composite unique keys or multiple unique keys. Can you recheck for the same.
when will this fix be released? any plan or a date? |
Is there any workaround or a temporary patch for v5? |
This is a much needed feature when can we get this released? |
Can we get this change in version 5? |
The issue, as I understand it, is that only conflicts on the primary key, as of now, will trigger the update on duplicate part, so if you have a primary key and a unique constraint, only the primary key will be taken into account, because it only gets the first one, so having more than one is no good. As a simple example, if you have the following table players and for the sake of simplicity we assume that a team cannot have two players with the same name, instead of adding the unique constraint, you can add a composite primary key like so
This doesn't solve case-insensitivity - I used foreign keys, so it wasn't an issue for me, but you might want to take this into account if its an issue for you. |
Can we get this on V5 please |
what can i do to get this in v5? |
Just tried this on v6 and this will fail if the composite is a unique index. as the this.uniqueKeys does not pickup composit indexes. So it looks like there is still some more work to do on this front. |
@brianschardt I can confirm it's still an issue for me as well for your same use case. Any other workarounds to achieve the same goal? |
This PR #12516 I just created should fix this issue |
Pull Request check-list
Please make sure to review and check all of these items:
npm run test
ornpm run test-DIALECT
pass with this change (including linting)?Description of change
I noticed that my original PR #11163 to support
updateOnDuplicate
for Postgres (and subsequent community updates since then) isn't working with composite unique keys. This fixes that, I think.This bug appears to have been raised in issues #11569 and #11534, and maybe #11665?
I think PR #11611 is supposed to fix this? But it looks like it also adds additional functionality (
upsertIndex
andupsertKeys
options onbulkCreate()
), and has not been merged yet.I added 2 tests as well: one for composite
primaryKey
bulkCreates, and one for compositeunique
bulkCreates. All Postgres tests appear to be passing.