diff --git a/lib/postgresql.js b/lib/postgresql.js index 398e5168..11a5f349 100644 --- a/lib/postgresql.js +++ b/lib/postgresql.js @@ -625,11 +625,13 @@ PostgreSQL.prototype._buildWhere = function(model, where) { branches.push(stmtForClause.sql); } } - stmt.merge({ - sql: '(' + branches.join(' ' + key.toUpperCase() + ' ') + ')', - params: branchParams, - }); - whereStmts.push(stmt); + if (branches.length > 0) { + stmt.merge({ + sql: '(' + branches.join(' ' + key.toUpperCase() + ' ') + ')', + params: branchParams, + }); + whereStmts.push(stmt); + } continue; } // The value is not an array, fall back to regular fields diff --git a/test/postgresql.test.js b/test/postgresql.test.js index 67e73b28..7448290e 100644 --- a/test/postgresql.test.js +++ b/test/postgresql.test.js @@ -442,6 +442,15 @@ describe('postgresql connector', function() { }); }); + it('should disregard empty and/or fields', function(done) { + Post.find({where: {and: []}}, function(err, post) { + should.not.exist(err); + should.exist(post); + post.length.should.not.equal(0); + done(); + }); + }); + it('should preserve order of and/or in where', async function() { await Post.create({title: 'T3', content: 'C3', approved: false}); // WHERE (title='T3' OR approved=false) AND (content='C2')