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

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

Closed
2 of 7 tasks
PrakharAkgec opened this issue Nov 16, 2019 · 6 comments · Fixed by #12453
Closed
2 of 7 tasks
Labels
dialect: mssql For issues and PRs. Things that involve MSSQL (and do not involve all dialects). released

Comments

@PrakharAkgec
Copy link

PrakharAkgec commented Nov 16, 2019

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.
@papb
Copy link
Member

papb commented 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

@papb papb added dialect: mssql For issues and PRs. Things that involve MSSQL (and do not involve all dialects). status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action labels Jan 15, 2020
@PrakharAkgec
Copy link
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....

@papb
Copy link
Member

papb commented Jan 15, 2020

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!!

@soryy708
Copy link
Contributor

#311

@patrickcarnahan
Copy link
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.

@sushantdhiman sushantdhiman removed the status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action label Jul 3, 2020
@sushantdhiman
Copy link
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
Labels
dialect: mssql For issues and PRs. Things that involve MSSQL (and do not involve all dialects). released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants