diff --git a/knex.js b/knex.js index 9a4a007e..ea107b2f 100644 --- a/knex.js +++ b/knex.js @@ -679,7 +679,7 @@ // Adds a `group by` clause to the query. groupBy: function() { - this.groups = this.groups.concat(_.toArray(arguments)); + this.groups = (this.groups||[]).concat(_.toArray(arguments)); return this; }, diff --git a/test/lib/aggregate.js b/test/lib/aggregate.js index c819c40e..3fd5f949 100644 --- a/test/lib/aggregate.js +++ b/test/lib/aggregate.js @@ -9,4 +9,12 @@ module.exports = function(Knex, dbName, resolver) { }); -}; \ No newline at end of file + it('has a count', function(ok) { + Knex('accounts').count('id').then(resolver(ok), ok); + }); + + it("support the groupBy function", function(ok) { + Knex('accounts').count('id').groupBy('logins').then(resolver(ok), ok); + }); + +}; diff --git a/test/shared/output.js b/test/shared/output.js index 7130711d..ca3a5986 100644 --- a/test/shared/output.js +++ b/test/shared/output.js @@ -504,6 +504,34 @@ module.exports = { bindings: [] } }, + 'aggregate.2': { + mysql: { + sql: ['select count(`id`) as aggregate from `accounts`'], + bindings: [] + }, + postgres: { + sql: ['select count("id") as aggregate from "accounts"'], + bindings: [] + }, + sqlite3: { + sql: ['select count("id") as aggregate from "accounts"'], + bindings: [] + } + }, + 'aggregate.3': { + mysql: { + sql: ['select count(`id`) as aggregate from `accounts` group by `logins`'], + bindings: [] + }, + postgres: { + sql: ['select count("id") as aggregate from "accounts" group by "logins"'], + bindings: [] + }, + sqlite3: { + sql: ['select count("id") as aggregate from "accounts" group by "logins"'], + bindings: [] + } + }, 'joins.1': { mysql: { sql: ['select `accounts`.*, `test_table_two`.`details` from `accounts` inner join `test_table_two` on `accounts`.`id` = `test_table_two`.`account_id`'], @@ -1961,6 +1989,34 @@ module.exports = { aggregate: 10 }] }, + 'aggregate.2': { + mysql: [{ + aggregate: 6 + }], + postgres: [{ + aggregate: 6 + }], + sqlite3: [{ + aggregate: 6 + }] + }, + 'aggregate.3': { + mysql: [{ + aggregate: 2 + }, { + aggregate: 4 + }], + postgres: [{ + aggregate: 2 + }, { + aggregate: 4 + }], + sqlite3: [{ + aggregate: 2 + }, { + aggregate: 4 + }] + }, 'joins.1': { mysql: [{ id: 1,