Skip to content
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

feat(postgres, sqlite): support where clause in 'on conflict do update set ... WHERE <condition>' #17144

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

DjellalAbdou
Copy link

Pull Request Checklist

  • Have you added new tests to prevent regressions?
  • If a documentation update is necessary, have you opened a PR to the documentation repository?
  • Did you update the typescript typings accordingly (if applicable)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Does the name of your PR follow our conventions?

Description of Changes

  • Added the option onConflictUpdateWhere to QueryGenerator.prototype.insertQuery and QueryGenerator.prototype.bulkInsertQuery. This option is used to generate the ON CONFLICT DO UPDATE SET... WHERE part of the query.

Example use case :

  await User.bulkCreate({ id: 'Abdou', score: '56', last_used_date: some_date }, {
    conflictAttributes: ['id'],
    updateOnDuplicate: ['score', 'last_used_date'],
    onConflictUpdateWhere: {
      last_used_date: { [Op.gte]: some_base_date },
    },
  });

Note: This option is only available for Postgres and SQLite

The resulting Query will be something like this :

INSERT INTO "Users" ("id", "score", "last_used_date") 
VALUES (DEFAULT, '33', '2024-02-29 17:43:48.530 +00:00'),
ON CONFLICT ("id") DO UPDATE SET 
	"score"=EXCLUDED."score",
	"last_used_date"=EXCLUDED."last_used_date" 
WHERE "Users"."last_used_date" <= (some_date_here)
RETURNING "id", "score", "last_used_date";

This feature was already discussed in this issue, Sequalize already has an opened PR treating the same issue but with a lot of conflicts to the current version of Sequelize.

@DjellalAbdou DjellalAbdou requested a review from a team as a code owner March 4, 2024 17:31
@DjellalAbdou
Copy link
Author

@WikiRik or @ephys can you take a look at this? I really need it 🙏

@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
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.

None yet

1 participant