Skip to content

feat: add on conflict do update where - #13155

Draft
aheuermann wants to merge 2 commits into
sequelize:mainfrom
aheuermann:on_conflict_do_update_where
Draft

aheuermann wants to merge 2 commits into
sequelize:mainfrom
aheuermann:on_conflict_do_update_where

Conversation

@aheuermann

@aheuermann aheuermann commented Mar 29, 2021 •

Copy link
Copy Markdown
Contributor

Closed #11656

Wanting some feedback on the API and can adjust. Mysql doesn't support a where clause for updates.

Pull Request check-list

Please make sure to review and check all of these items:

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you update the typescript typings accordingly (if applicable)?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

I'd like to be able to include a where clause in the a postgres upsert INSERT ON CONFLICT DO UPDATE statement. This is not currently possibly with sequelize. 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_tsp

Comment thread lib/model.js
Comment on lines +2562 to +2564
if (options.updateOnDuplicate && options.where && dialect !== 'postgres') {
throw new Error(`${dialect} does not support the where clause option for bulkCreate.`);
}

@wbourne0 wbourne0 Aug 4, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both sqlite and postgres support this.

Also:

There's 2 clauses which might show up for both dialects in an ON CONFLICT DO UPDATE statement:

  • ON CONFLICT WHERE - This is used for indices, only references to the target table are allowed. E.g. ON CONFLICT WHERE "username" vs ON CONFLICT WHERE "users"."username".
  • ON CONFLICT DO UPDATE WHERE - This doesn't help with indices - however it allows for more advanced clause for when to update.

@wbourne0 wbourne0 Aug 4, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nvm, its actually onConflictDoUpdateWhere not where. Might be worth simplifying this term, I'm using conflictWhere for the first WHERE - maybe updateWhere?

@clarencedesmarais

clarencedesmarais commented Sep 12, 2022 •

Copy link
Copy Markdown

Can we re-evaluate if we can merge this in ? This is a very useful feature.

@ephys ephys changed the title feat(on-conflict-where): add on confluct do update where fixes #11656 feat: add on conflict do update where Mar 24, 2023
@ephys
ephys requested a review from a team March 24, 2023 10:23
@aheuermann
aheuermann requested a review from a team as a code owner March 18, 2024 23:04
@aheuermann
aheuermann requested review from WikiRik and ephys and removed request for a team March 18, 2024 23:04
@sequelize-bot sequelize-bot Bot added the conflicted This PR has merge conflicts and will not be present in the list of PRs to review label Apr 10, 2024
@sequelize-bot
sequelize-bot Bot marked this pull request as draft October 26, 2026 00:27
@sequelize-bot

sequelize-bot Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Warning

This pull request has been automatically converted to draft because it is not ready to be merged.

Reason(s):

  • 🔀 Merge conflicts — This PR has merge conflicts that must be resolved before it can be merged.
  • 🔄 Unaddressed change requests — One or more reviewers have requested changes that have not been addressed yet. Once you have made the requested changes, please re-request a review from those reviewers.

Once you have addressed the issue(s) above, you may
mark this pull request as ready for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

conflicted This PR has merge conflicts and will not be present in the list of PRs to review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Postgres: INSERT ON CONFLICT DO UPDATE with WHERE clause

5 participants