Skip to content

Unable to insert/upsert record in MS SQL DB when a composite key has a falsey value #11684

Closed

Description

@PrakharAkgec

Issue Description

While inserting a record in MS SQL DB using sequelize, if the table has a group of Primary Keys (Composite key), and the record to be inserted/upserted has a "falsey" value (false/0), then the record is not inserted/upserted.

What are you doing?

I was using sequelize for inserting/ upserting data in MS SQL DB, in which there was a composite key column that has a falsy value (eg. false/0).

It was throwing an error:

Error: Primary Key or Unique key should be passed to upsert query

Here is the link to the SSCCE for this issue: LINK-HERE

//model
module.exports = (sequelize, DataTypes) => {
    var Table_Test = sequelize.define("test_table", {
        Name: {
            type: DataTypes.STRING,
            primaryKey: true
        },
        Age: {
            type: DataTypes.INTEGER
        },
        IsOnline: {
            type: DataTypes.BOOLEAN,
            primaryKey: true
        }
    }, {
        freezeTableName: true,
        timestamps : false
    });
    return Table_Test;
};

//query
async function saveMessage(dbObj, schema) {
    return db[schema].upsert(dbObj)
        .then(() => {
            console.log(`:::::successfully saved data to mssql:::::`);
            return true;
        })
        .catch((err) => {
            return Promise.reject(err.parent);
        });
}

saveMessage({
    "Name": "Charlie",
    "Age": 24,
    "IsOnline": false
});

What do you expect to happen?

The record must be upserted in DB.

Output: :::::successfully saved data to mssql:::::

What is actually happening?

An error is returned stating, we can not skip the value for a primary key.

Returning this error:

{ ERROR: Error: Primary Key or Unique key should be passed to upsert query
    at MSSQLQueryGenerator.upsertQuery (/home/abc/node_modules/sequelize/lib/dialects/mssql/query-generator.js:462:13)
    ...
}

Additional context

Add any other context or screenshots about the feature request here.

Environment

  • Sequelize version: 5.15.0
  • Node.js version: v8.11.1
  • Operating System: Linux
  • If TypeScript related: TypeScript version: XXX

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): MSSQL
  • 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. papb commented on Jan 15, 2020

    @papb
    Member

    Hello, thanks for the report.

    Yes, I have the time and I know how to start.

    Are you still up for this? Let me know, thanks

  2. added
    dialect: mssql
    status: awaiting response
    on Jan 15, 2020
  3. PrakharAkgec commented on Jan 15, 2020

    @PrakharAkgec
    Author

    Hello, thanks for the report.

    Yes, I have the time and I know how to start.

    Are you still up for this? Let me know, thanks

    Yes, I am.... Would love to contribute....

  4. papb commented on Jan 15, 2020

    @papb
    Member

    Thank you 😬 that's great. The first thing I'd like to ask is for you to take the code you wrote in your first post and make a complete
    sequelize-sscce out of it, can you do that? Thanks!!

  5. soryy708 commented on May 21, 2020

    @soryy708
    Contributor
  6. patrickcarnahan commented on Jul 2, 2020

    @patrickcarnahan
    Contributor

    @papb i see this has had no traction in quite some time. we are currently hitting this while attempting to use an mssql dialect for azurite, an azure storage emulator. i'm happy to take up this work if it's not on any one else's plate.

  7. removed
    status: awaiting response
    on Jul 3, 2020
  8. sushantdhiman commented on Jul 4, 2020

    @sushantdhiman
    Contributor

    🎉 This issue has been resolved in version 6.3.0 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

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

    dialect: mssqlreleased

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions