question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
PrakharAkgeccommented, Jan 15, 2020

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…

0reactions
sushantdhimancommented, Jul 4, 2020

🎉 This issue has been resolved in version 6.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Read more comments on GitHub >

github_iconTop Results From Across the Web

What's wrong with nullable columns in composite primary keys?
A NULL is an unknown, whereas an empty string is a deliberate record of "something not being present". In other words, "empty" and...
Read more >
Composite Key in SQL: Your Ultimate Guide to Mastery
Composite keys in SQL prove to be useful in those cases where you have a requirement of keys that can uniquely identify records...
Read more >
Task Factory Release Notes | SentryOne
Fix 17481: Dimension Merge transform that uses a composite key with bit values throws "Duplicate Business Key in Source_System: NullKeyStruct" ...
Read more >
What's New in SQLAlchemy 2.0?
The Sequence construct reverts to not having any explicit default “start” value; impacts MS SQL Server¶. Prior to SQLAlchemy 1.4, the Sequence construct...
Read more >
sqlite_utils Python library - sqlite-utils - Datasette
SQLite supports cross-database SQL queries, which can join data from ... If the table has a compound primary key you can pass in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found