-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Upsert does not always find/handle unique index #13240
Comments
Add support for where clauses used in the ON CONFLICT part of the queries generated by model.bulkCreate and model.upsert for usage > Closes sequelize#13240, sequelize#13066, sequelize#13031, sequelize#12774, sequelize#12742, sequelize#12595, sequelize#11656
Can you break this up into multiple issues? I'm creating an issue for the partial index part, but ideally each step would be its own issue. Edit: Here's the issue for the |
This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂 |
Issue Description
What are you doing?
What do you expect to happen?
In the above code, I would expect the User model to be upserted based on the partial unique index.
What is actually happening?
Sequelize fails to detect the unique index and upserts based on primary key. So Postgres rejects the insert due to unique constraint without explicit handling.
Additional context
To my understanding, here are the general causes to this failure:
The first is that the index detection only looks for string literals for the field name https://github.com/sequelize/sequelize/blob/main/lib/dialects/abstract/query-interface.js#L789
So when an index uses functions, it cannot find the values (because
fn() !== fn()
)The second is that even if the unique-index selection logic found util methods, it would still fail to stringify the values (currently it only expects string literals).
https://github.com/sequelize/sequelize/blob/main/lib/dialects/abstract/query-generator.js#L186
(Will open PR for this fix)
And third is that only the fields are used in declaring the conflict. An index with a
where
clause should also have that included.See
index_predicate
here https://www.postgresql.org/docs/13/sql-insert.htmlEnvironment
Issue Template Checklist
How does this problem relate to dialects?
Would you be willing to resolve this issue by submitting a Pull Request?
I will open a PR for the second part, but the first and third are a bit trickier.
For the first, I think the best solution is to provide the name of the index in the options, and map that to the found index. Note the generated SQL should still use
unique index inference
for the reason stated in Postgres docs, but those issues don't apply to Sequelize.For the third, I think the insert query options needs to take a
where
option as well. And when present, stringify it and attach to the ON CONFLICT statement.The text was updated successfully, but these errors were encountered: