Skip to content

MSSQL UniqueConstraintError does not match for primary keys #8973

Closed

Description

@smassin

What are you doing?

Using model.findOrCreate on a table with a composite primary key will produce a generic database error because the unique key violation check does not check for primary key violations.

Model.findOrCreate(...)

What do you expect to happen?

findOrCreate should not produce a database error due to a unique key violation. It should requery and return an instance.

What is actually happening?

findOrCreate will produce the following error when inserting under high load:

SequelizeDatabaseError: Violation of PRIMARY KEY constraint '...'. Cannot insert duplicate key in object '...'. The duplicate key value is (..., ...)."

What is the solution?

match = err.message.match(/Violation of UNIQUE KEY constraint '((.|\s)*)'. Cannot insert duplicate key in object '.*'.(:? The duplicate key value is \((.*)\).)?/);

Needs to be:

match = err.message.match(/Violation of (UNIQUE|PRIMARY) KEY constraint '((.|\s)*)'. Cannot insert duplicate key in object '.*'.(:? The duplicate key value is \((.*)\).)?/); 

Tested working.

Dialect: mssql
Dialect version: Latest
Database version: Azure SQL
Sequelize version: Latest
Tested with latest release: Latest

Activity

  1. added
    type: bug
    good first issue
    on Jan 31, 2018
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

    good first issuetype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions