Skip to content

Upsert where condition matching #13031

Closed
Closed

Description

@IlyaDonskikh

Issue Description

When i create an index with where condition it does not match ON CONFLICT statement on upsert.

What are you doing?

PSQL Index:

CREATE UNIQUE INDEX ON "lists" ("sample", "example") WHERE ("deletedAt" IS NULL);

Model Index:

indexes: [
  {
    unique: true,
    fields: ['sample', 'example'],
    where: {
      deletedAt: null,
    },
  },
]

Request:

 await List.upsert(attributes, { transaction });

What do you expect to happen?

It have to INSERT new record or UPDATE existing one based on uniq index condition.

INSERT INTO "lists" ("sample","example","createdAt","updatedAt") VALUES ($1,$2,$3,$4) 
  ON CONFLICT ("sample","example") WHERE deletedAt is NULL DO...

What is actually happening?

Error:

original: error: there is no unique or exclusion constraint matching the ON CONFLICT specification

INSERT INTO "lists" ("sample","example","createdAt","updatedAt") VALUES ($1,$2,$3,$4) 
  ON CONFLICT ("sample","example") DO UPDATE SET...

Additional context

Seems like ON CONFLICT WHERE case does not covered. At least according to following PR code.

So, instead of mapping where condition into ON CONFLICT statement, the builder just ignore it.

Environment

  • Sequelize version: 6.3.5
  • Node.js version: v12.16.1
  • Operating System: MacOs 10.15.7

Issue Template Checklist

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s): Psql
  • I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX

Would you be willing to resolve this issue by submitting a Pull Request?

  • 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

  1. deggertsen commented on Feb 20, 2021

    @deggertsen

    Ran into this issue today. Upsert fails where there is a unique constraint rather than simply updating the record with the new values. Tried to setup the unique constraint in an index like OP and then tried it as a separate constraint, both failed.

    severity: 'ERROR', code: '23505', detail: 'Key (keys, content_id, content_type)=(Mount Pleasant, 1111111, xxxxx) already exists.',

  2. github-actions commented on Oct 27, 2021

    @github-actions
    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. 🙂

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions