Skip to content

feat(postgres, sqlite): add support for where clauses with ON CONFLICT queries - #13270

Closed
wbourne0 wants to merge 6 commits into
sequelize:mainfrom
wbourne0:wb-on-conflict-upsert
Closed

wbourne0 wants to merge 6 commits into
sequelize:mainfrom
wbourne0:wb-on-conflict-upsert

Conversation

@wbourne0

@wbourne0 wbourne0 commented May 11, 2021 •

Copy link
Copy Markdown
Member

Add support for where clauses used in the ON CONFLICT part of the queries generated by model.bulkCreate and model.upsert for usage >

Pull Request check-list

  • 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

This is similar to #11611 but a bit different. Unique indexes partial on the records not being soft deleted (eg, CREATE UNIQUE INDEX 'abc' ON 'table1' ("col1") WHERE time_deleted IS NULL) are picked up automatically so long as:

  1. All of the fields in the index are being specified in conflictFields (for upsert, upsertFields for bulkCreate, happy to change naming if needed)
  2. The index is in the model's definition.

Model.upsert has 2 more options:

  • conflictFields - This is for specifying the keys which are added in ON CONFLICT(). (eg, ON CONFLICT(${conflictFields})).
  • conflictWhere - This is used to specify a where clause for the ON CONFLICT part of the query.

Model.bulkCreate has the same options but with different names (might be worth changing them to match):

  • upsertFields - Same thing as conflictFields.
  • upsertWhere - Same as conflictWhere.
    Note: bulkCreate will ignore these options if updateOnDuplicate isn't specified.

An example case of where this is handy:

// Create a table for Users
// Users need unique usernames, but only if they haven't been deleted.
const Users = sequelize.define('users', {
    username: { type: DataTypes.CITEXT },
    email: { type: DataTypes.CITEXT },
    time_deleted: { type: DataTypes.DATE }, 
}, {
    paranoid: true,
    deletedAt: 'time_deleted',
    indexes: [{
        fields: ['username'],
        unique: true,
        where: { time_deleted: null },
    }],
});

await Users.upsert({
    username: 'User123',
    email: '[email protected]' 
}, {
    conflictFields: ['username'],
});

Closes #13240, #13066, #13031, #12774, #12742, #12595, #11656

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

Copy link
Copy Markdown

This feature will help us out, we currently have a custom upsert function that does this that this would replace

@wbourne0 wbourne0 added dialect: postgres For issues and PRs. Things that involve PostgreSQL (and do not involve all dialects). dialect: sqlite For issues and PRs. Things that involve SQLite (and do not involve all dialects). type: feature DEPRECATED: replace with the "feature" issue type and removed type: feature DEPRECATED: replace with the "feature" issue type labels May 26, 2021
@wbourne0
wbourne0 force-pushed the wb-on-conflict-upsert branch from fe7d513 to 707371e Compare May 30, 2021 00:36
mohanagy
mohanagy previously approved these changes Jun 7, 2021
@papb

papb commented Jun 26, 2021

Copy link
Copy Markdown
Member

Hello! Just to have it written here, as we discussed earlier on Slack: please split this PR in four parts, considering #13346 to be the first of them. The other three parts should be:

Note: maybe you can join the last two in a single PR if you want.

@papb papb added status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action type: feature DEPRECATED: replace with the "feature" issue type labels Jun 26, 2021
@wbourne0

wbourne0 commented Jul 4, 2021

Copy link
Copy Markdown
Member Author

Sounds good, going to close this now and break it up. I'll follow up and mention the other PRs once I make them.

@wbourne0 wbourne0 closed this Jul 4, 2021
@github-actions github-actions Bot removed the status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action label Jul 4, 2021
@wbourne0 wbourne0 mentioned this pull request Aug 4, 2021
1 of 2 tasks
@wbourne0

wbourne0 commented Aug 4, 2021

Copy link
Copy Markdown
Member Author

This feature will help us out, we currently have a custom upsert function that does this that this would replace

#13412 should take care of that. I'm currently working on a similar pr for bulkCreate.

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

Labels

dialect: postgres For issues and PRs. Things that involve PostgreSQL (and do not involve all dialects). dialect: sqlite For issues and PRs. Things that involve SQLite (and do not involve all dialects). type: feature DEPRECATED: replace with the "feature" issue type

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Upsert does not always find/handle unique index

5 participants