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

ON CONFLICT WHERE for upserts. #13412

Closed
1 of 2 tasks
wbourne0 opened this issue Aug 4, 2021 · 4 comments · Fixed by #13411
Closed
1 of 2 tasks

ON CONFLICT WHERE for upserts. #13412

wbourne0 opened this issue Aug 4, 2021 · 4 comments · Fixed by #13411
Assignees
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). P4: nice to have For issues that are not bugs. status: wip For issues and PRs. Applied when the PR is not ready yet / when work to close the issue has started. type: feature For issues and PRs. For new features. Never breaking changes.

Comments

@wbourne0
Copy link
Member

wbourne0 commented Aug 4, 2021

Issue Creation Checklist

[x] I have read the contribution guidelines

Feature Description

Currently sequelize doesn't support upserts with unique partial indexes.

There's many cases for unique partial indexes - such as a unique constraint on rows that haven't been soft deleted.

For example, a generic membership table with permissions:

const Memberships = sequelize.define(
  'memberships',
  {
    user_id: { type: DataTypes.INTEGER, allowNull: false },
    foreign_key: { type: DataTypes.INTEGER, allowNull: false },
    permissions: { type: DataTypes.ENUM('admin', 'member', 'guest'), allowNull: false }
  },
  {
    deletedAt: 'time_deleted',
    indexes: [
      {
        fields: ['user_id', 'foreign_key'],
        unique: true,
        where: { time_deleted: null },
      },
    ],
  }
);

// Adds a membership for a user as an admin (updates previous membership instead if there is one)
await Memberships.upsert(
  {
    user_id: userId,
    foreign_id: foreignId,
    permissions: 'admin',
  },
  {
    conflictWhere: { time_deleted: null },
  }
);

Is your feature request related to a problem? Please describe.

There's several cases where this comes up:

  • Unique usernames, except for deleted users.
  • Membership models - something can't belong to the same item multiple times, however it can be fine for the association to be deleted then created later.

Describe the solution you'd like

See the code sample above.

Additional context

Part of #13240.

I also have a PR up at #13411, which is a piece of #13270 (closed)

Feature Request Checklist

Is this feature dialect-specific?

  • No. This feature is relevant to Sequelize as a whole.
  • Yes. This feature only applies to the following dialect(s): Postgres, Sqlite

Would you be willing to implement this feature by submitting a Pull Request?

#13411

@wbourne0 wbourne0 added type: feature For issues and PRs. For new features. Never breaking changes. dialect: sqlite For issues and PRs. Things that involve SQLite (and do not involve all dialects). dialect: postgres For issues and PRs. Things that involve PostgreSQL (and do not involve all dialects). status: wip For issues and PRs. Applied when the PR is not ready yet / when work to close the issue has started. labels Aug 4, 2021
@wbourne0 wbourne0 self-assigned this Aug 4, 2021
@wbourne0 wbourne0 linked a pull request Aug 4, 2021 that will close this issue
6 tasks
@wbourne0 wbourne0 added the P4: nice to have For issues that are not bugs. label Aug 5, 2021
@github-actions
Copy link
Contributor

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

@github-actions github-actions bot added the stale label Oct 27, 2021
@wbourne0 wbourne0 removed the stale label Oct 31, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Nov 7, 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. 🙂

@github-actions github-actions bot added the stale label Nov 7, 2021
@WikiRik WikiRik removed the stale label Nov 15, 2021
@mschoeffmann
Copy link

+1 for this one

@pavhcoinstats
Copy link

Hello

I have to similar problems

It will be good set array of conflicted fields on upsert

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). P4: nice to have For issues that are not bugs. status: wip For issues and PRs. Applied when the PR is not ready yet / when work to close the issue has started. type: feature For issues and PRs. For new features. Never breaking changes.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants