Repository navigation
Description
Issue Description
I'd like to be able to include a where clause in the a postgres upsert INSERT ON CONFLICT DO UPDATE statement. Something like:
INSERT INTO
person (name, last_action_tsp)
values ('steve', '2019-11-06T15:41:47.249Z')
ON CONFLICT DO UPDATE
SET name=EXCLUDED.name, last_action_tsp=EXCLUDED.last_action_tsp
WHERE
EXCLUDED.last_action_tsp > last_action_tspIs your feature request related to a problem? Please describe.
We only want to upsert if the timestamp (last_action_tsp) is greater than the value currently in the database.
Describe the solution you'd like
I think an api like this could work but open to suggestions.
Model.bulkCreate(data, {
updateOnDuplicate: {
columns: ['name', 'last_action_tsp'],
where: {
lastActionTsp: {
[Op.lt]: literal('EXCLUDED.last_action_tsp'),
},
},
},
});Why should this be in Sequelize
This should be in sequelize since it is a supported by postgres but it looks like it may not be supported by mysql.
Describe alternatives/workarounds you've considered
The only workaround I can think of is to run an insert followed by an update with the condition but this results in two queries when ideally this could be done in one.
Issue Template Checklist
Is this issue dialect-specific?
- No. This issue is relevant to Sequelize as a whole.
- Yes. This issue only applies to the following dialect(s): postgres
Would you be willing to resolve this issue by submitting a Pull Request?
I implemented this based on a pervious PR inDream#4
I know the merged implementation is different than the one proposed, but I am comfortable diving in and implementing this if you are open to it.
- Yes, I have the time and I know how to start.
- Yes, I have the time but I don't know how to start, I would need guidance.
- No, I don't have the time, although I believe I could do it if I had the time...
- No, I don't have the time and I wouldn't even know how to start.
Activity
[-]updateOnDuplicate with where clause[/-][+]Postgres: INSERT ON CONFLICT DO UPDATE with WHERE clause[/+]papb commentedon Jan 15, 2020
Hello, sorry to take long to reply to your issue; this looks like a good think to exist...
Hi @thaddeusmt, sorry to bother you, what do you think of this? Can you give an opinion? Thanks :)
feat(on-conflict-where): add on confluct do update where fixes sequel…
feat(on-conflict-where): add on confluct do update where fixes sequel…
feat(on-conflict-where): add on confluct do update where fixes sequel…
feat(on-conflict-where): add on conflict do update where fixes sequel…
feat(model): add support for where clauses with ON CONFLICT queries
github-actions commentedon Nov 5, 2021
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. 🙂
AnthonyRoss commentedon Jun 1, 2022
Any update on this issue? Do we need to open a PR for this to get the feature added?
Rochet2 commentedon Sep 2, 2022
This would be useful for me for the exact same use case mentioned in the issue description.
Seems that there is already a PR for this, but it was automatically closed as stale.
clarencedesmarais commentedon Sep 12, 2022
Very useful to me too. :)
IslamWahid commentedon Nov 29, 2023
needed here as well!
Ferido07 commentedon Jan 14, 2025
I used
conflictWherelikeon sequelize version 6.33.0 and it worked. Although it is not documented in https://sequelize.org/api/v6/class/src/model.js~model#static-method-bulkCreate. I got the information from https://sequelize.org/api/v7/interfaces/_sequelize_core.index.bulkcreateoptions#conflictWhere which is for version 7 but works on version 6.33 as well.