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

fix(mssql): upsert query with falsey values #12453

Merged
merged 9 commits into from
Jul 3, 2020
2 changes: 1 addition & 1 deletion src/dialects/mssql/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
* Exclude NULL Composite PK/UK. Partial Composite clauses should also be excluded as it doesn't guarantee a single row
*/
for (const key in clause) {
if (!clause[key]) {
if (typeof clause[key] === 'undefined' || clause[key] == null) {
valid = false;
break;
}
Expand Down
49 changes: 49 additions & 0 deletions test/unit/dialects/mssql/query-generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const Support = require('../../support');
const expectsql = Support.expectsql;
const current = Support.sequelize;
const DataTypes = require('../../../../lib/data-types');
const Op = require('../../../../lib/operators');
const TableHints = require('../../../../lib/table-hints');
const QueryGenerator = require('../../../../lib/dialects/mssql/query-generator');

Expand All @@ -22,6 +24,53 @@ if (current.dialect.name === 'mssql') {
});
});

it('upsertQuery with falsey values', function () {
let testTable = this.sequelize.define(
'test_table',
{
Name: {
type: DataTypes.STRING,
primaryKey: true
},
Age: {
type: DataTypes.INTEGER
},
IsOnline: {
type: DataTypes.BOOLEAN,
primaryKey: true
}
},
{
freezeTableName: true,
timestamps: false
}
);

const insertValues = {
Name: 'Charlie',
Age: 24,
IsOnline: false
};

const updateValues = {
Age: 24
};

const whereValues = [
{
Name: 'Charlie',
IsOnline: false
}
];

const where = {
[Op.or]: whereValues
};

// the main purpose of this test is to validate this does not throw
this.queryGenerator.upsertQuery('test_table', updateValues, insertValues, where, testTable);
patrickcarnahan marked this conversation as resolved.
Show resolved Hide resolved
});

it('createDatabaseQuery with collate', function () {
expectsql(
this.queryGenerator.createDatabaseQuery('myDatabase', {
Expand Down