Skip to content

Commit

Permalink
fix(selectQuery): don't add empty HAVING clause (#8931)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorrit authored and sushantdhiman committed Jan 22, 2018
1 parent a4a0f88 commit 335efd6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/dialects/abstract/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1159,10 +1159,12 @@ const QueryGenerator = {
// Add HAVING to sub or main query
if (options.hasOwnProperty('having')) {
options.having = this.getWhereConditions(options.having, tableName, model, options, false);
if (subQuery) {
subQueryItems.push(' HAVING ' + options.having);
} else {
mainQueryItems.push(' HAVING ' + options.having);
if (options.having) {
if (subQuery) {
subQueryItems.push(' HAVING ' + options.having);
} else {
mainQueryItems.push(' HAVING ' + options.having);
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions test/unit/dialects/mysql/query-generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,28 @@ if (dialect === 'mysql') {
arguments: ['myTable', {where: {field: {$notRegexp: '^[h|a|t]'}}}],
expectation: "SELECT * FROM `myTable` WHERE `myTable`.`field` NOT REGEXP '^[h|a|t]';",
context: QueryGenerator
}, {
title: 'Empty having',
arguments: ['myTable', function() {
return {
having: {}
};
}],
expectation: 'SELECT * FROM `myTable`;',
context: QueryGenerator,
needsSequelize: true
}, {
title: 'Having in subquery',
arguments: ['myTable', function() {
return {
subQuery: true,
tableAs: 'test',
having: { creationYear: { [Operators.gt]: 2002 } }
};
}],
expectation: 'SELECT `test`.* FROM (SELECT * FROM `myTable` AS `test` HAVING `creationYear` > 2002) AS `test`;',
context: QueryGenerator,
needsSequelize: true
}
],

Expand Down

0 comments on commit 335efd6

Please sign in to comment.