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

upsert fails on MSSQL with composite unique key #11240

Closed
2 of 7 tasks
massivex opened this issue Jul 29, 2019 · 2 comments
Closed
2 of 7 tasks

upsert fails on MSSQL with composite unique key #11240

massivex opened this issue Jul 29, 2019 · 2 comments
Assignees
Labels
dialect: mssql For issues and PRs. Things that involve MSSQL (and do not involve all dialects). type: bug

Comments

@massivex
Copy link

massivex commented Jul 29, 2019

What are you doing?

upsert in MSSQL with composite unique key

// Define a Model with a composite unique key
class Project extends Sequelize.Model { }
Project.init({
    department: { type: Sequelize.STRING(20), unique: 'ak__project', allowNull: false },
    code: { type: Sequelize.STRING(20), unique: 'ak__project', allowNull: false }
}, { sequelize, modelName: 'project' });

Project.upsert({ department: 'DEV', code: '' }); // => Error: Primary Key or Unique key should be passed to upsert query

To Reproduce
Steps to reproduce the behavior:

  1. Define model with a composite key with at least a string field
  2. Execute an upsert with a key set to empty string
  3. See error Primary Key or Unique key should be passed to upsert query

What do you expect to happen?

Unique key values specified must be used as where even if a key value is an empty string

What is actually happening?

Sequelize mssql dialect exclude unique keys value from where because one of the key values is an empty string.
This behaviour happens because empty string is evaluated falsy exactly at

Environment

Dialect:

  • mysql
  • postgres
  • sqlite
  • mssql
  • any

Dialect tedious version: 5.0.3
Database version: SQL SERVER 2017 - 14.0.3045.24
Sequelize version: 4.44.2
Node Version: 8.11.2
OS: Linux
If TypeScript related: TypeScript version: XXX

Tested with latest release:

  • No
  • Yes, specify that version: 5.11.0
@massivex
Copy link
Author

massivex commented Jul 29, 2019

I suggest to change this line


with

if (typeof clause[key] === 'undefined' || clause[key] == null) {

to esclude undefined and null values but keep string empty and other falsy values like 0 and false that are both valid for unique key values.

@papb papb added dialect: mssql For issues and PRs. Things that involve MSSQL (and do not involve all dialects). status: understood For issues. Applied when the issue is understood / reproducible. type: bug labels Jul 29, 2019
@papb papb self-assigned this Jul 29, 2019
@sushantdhiman
Copy link
Contributor

Fixed by #12453

@sushantdhiman sushantdhiman removed the status: understood For issues. Applied when the issue is understood / reproducible. label Jul 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dialect: mssql For issues and PRs. Things that involve MSSQL (and do not involve all dialects). type: bug
Projects
None yet
Development

No branches or pull requests

2 participants