Skip to content

Commit 9422df0

Browse files
authored
Merge pull request #420 from strongloop/fixbuildwhere
fix: `DEFAULT` for null values in where clause
2 parents a6852c9 + ec08dea commit 9422df0

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

lib/postgresql.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -644,10 +644,10 @@ PostgreSQL.prototype._buildWhere = function(model, where) {
644644
if (Array.isArray(expression)) {
645645
// Column value is a list
646646
for (let j = 0, m = expression.length; j < m; j++) {
647-
columnValue.push(this.toColumnValue(p, expression[j]));
647+
columnValue.push(this.toColumnValue(p, expression[j], true));
648648
}
649649
} else {
650-
columnValue.push(this.toColumnValue(p, expression));
650+
columnValue.push(this.toColumnValue(p, expression, true));
651651
}
652652
if (operator === 'between') {
653653
// BETWEEN v1 AND v2
@@ -669,7 +669,7 @@ PostgreSQL.prototype._buildWhere = function(model, where) {
669669
// do not coerce RegExp based on property definitions
670670
columnValue = expression;
671671
} else {
672-
columnValue = this.toColumnValue(p, expression);
672+
columnValue = this.toColumnValue(p, expression, true);
673673
}
674674
sqlExp = self.buildExpression(columnName, operator, columnValue, p);
675675
stmt.merge(sqlExp);
@@ -711,13 +711,15 @@ PostgreSQL.prototype._buildWhere = function(model, where) {
711711
* Convert property name/value to an escaped DB column value
712712
* @param {Object} prop Property descriptor
713713
* @param {*} val Property value
714+
* @param {boolean} isWhereClause
714715
* @returns {*} The escaped value of DB column
715716
*/
716-
PostgreSQL.prototype.toColumnValue = function(prop, val) {
717+
PostgreSQL.prototype.toColumnValue = function(prop, val, isWhereClause) {
717718
if (val == null) {
718719
// PostgreSQL complains with NULLs in not null columns
719720
// If we have an autoincrement value, return DEFAULT instead
720-
if (prop.autoIncrement || prop.id) {
721+
// Do not return 'DEFAULT' for id field in where clause
722+
if (prop.autoIncrement || (prop.id && !isWhereClause)) {
721723
return new ParameterizedSQL('DEFAULT');
722724
} else {
723725
return null;

test/postgresql.filterUndefined.test.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'use strict';
77
const should = require('should'),
88
assert = require('assert');
9-
let Post, db;
9+
let Post, db, Charge, Currency;
1010

1111
describe('filter undefined fields', function() {
1212
before(function() {
@@ -136,4 +136,55 @@ describe('filter undefined fields', function() {
136136
});
137137
});
138138
});
139+
140+
describe('able to handle null foreign keys', function() {
141+
before(function(done) {
142+
Charge = db.define('Charge', {amount: Number});
143+
Currency = db.define('Currency', {code: String, country: String});
144+
Charge.belongsTo('Currency', {as: 'currency'});
145+
db.automigrate(['Charge', 'Currency'], done);
146+
});
147+
148+
it('able to create entity with null foreign key', function(done) {
149+
Currency.create({code: 'USD', country: 'USA'}, function(err, currency) {
150+
if (err) return done(err);
151+
Charge.create({amount: 10, currencyId: currency.id}, function(err) {
152+
if (err) return done(err);
153+
Charge.create({amount: 10, currencyId: null}, function(err, charge) {
154+
if (err) return done(err);
155+
should.not.exist(charge.currencyId);
156+
done();
157+
});
158+
});
159+
});
160+
});
161+
162+
it('able to query entities with null foreign key', function(done) {
163+
Charge.find({
164+
where: {currency: {
165+
inq: [null, 1],
166+
}},
167+
include: {relation: 'currency'},
168+
}, function(err, charges) {
169+
if (err) return done(err);
170+
charges.should.have.lengthOf(2);
171+
done();
172+
});
173+
});
174+
175+
it('able to filter entities with null id condition', function(done) {
176+
Currency.find({
177+
where: {
178+
id: {
179+
inq: [null, 1],
180+
},
181+
},
182+
},
183+
function(err, currencies) {
184+
if (err) return done(err);
185+
currencies.should.have.lengthOf(1);
186+
done();
187+
});
188+
});
189+
});
139190
});

0 commit comments

Comments
 (0)