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
Invalid ON CONFLICT written for upsert on models with conditional indexes (v6) #12595
Comments
There is also a similar issue when using composite fields that require coalescing in the index. If I try to create an index like so:
The index is never used because the queryInterface is searching the "fields" array for a stringified column name instead of detecting that the field may be a sequelize method and parsing the column name from that.
|
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
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. 🙂 |
Still an issue for me :) |
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. 🙂 |
Still an issue. |
+1 |
1 similar comment
+1 |
still an issue |
+1 |
2 similar comments
+1 |
+1 |
I tried the following and its working for me: Model.upsert({
property1: 1,
property2: "sadasd",
property3: 32323
}, {
conflictFields: ['property1', 'property2']
}) |
awesome thanks works for me |
For Model.upsert({
conversationId: 'my-id',
name: 'My conversation',
}, {
conflictFields: ['conversationId'],
conflictWhere: { deletedAt: null },
}); |
Issue Description
In version 6 query generation was made aware of the ON CONFLICT clause as is available in postgresql/sqlite. However, when upsert() is used in a model that has an index over two columns that is conditional on a third column, the conditional clause is not included in the ON CONFLICT and thus the upsert is rejected.
What are you doing?
The following is a minimal reproduction:
What do you expect to happen?
The upsert() to successfully complete.
What is actually happening?
Additional context
As issued, the INSERT query looks as follows:
Based on some reading of the postgresql docs (), it appears a
WHERE index_predicate
is required betweenON CONFLICT DO UPDATE
to be able to disambiguate the conditional index. Thus in order for this query to succeed for this model, it must be written as follows:When I looked in the code, the first unique index matching the columns being is chosen but then only the index keys are passed down into query generator
insertQuery()
function which writes them out asON CONFLICT (...keys...)
, but the information is no longer available to it that there was a condition on the index so this is effectively ignored.Environment
Issue Template Checklist
How does this problem relate to dialects?
I think this problem happens only for the following dialect(s): postresql, sqlite
Would you be willing to resolve this issue by submitting a Pull Request?
I'd be happy to work on a PR, but given the way this is currently handled an approach to addressing that the maintainers are comfortable with would likely need to be agreed.
The text was updated successfully, but these errors were encountered: