diff --git a/lib/query/querycompiler.js b/lib/query/querycompiler.js index 6e9084485e..784232e240 100644 --- a/lib/query/querycompiler.js +++ b/lib/query/querycompiler.js @@ -702,7 +702,11 @@ class QueryCompiler { // If we haven't specified any columns or a `tableName`, we're assuming this // is only being used for unions. onlyUnions() { - return !this.grouped.columns && this.grouped.union && !this.tableName; + return ( + (!this.grouped.columns || !!this.grouped.columns[0].value) && + this.grouped.union && + !this.tableName + ); } limit() { diff --git a/test/unit/query/builder.js b/test/unit/query/builder.js index 9c32f063df..9ba0246db8 100644 --- a/test/unit/query/builder.js +++ b/test/unit/query/builder.js @@ -2205,6 +2205,36 @@ describe('QueryBuilder', () => { bindings: [1, 2, 3], }, }); + + // Issue #4364 + const firstUnionAll = qb() + .unionAll([ + function () { + this.select().from('users').where({ id: 1 }); + }, + function () { + this.select().from('users').where({ id: 2 }); + }, + ]) + .first(); + testsql(firstUnionAll, { + mysql: { + sql: 'select * from `users` where `id` = ? union all select * from `users` where `id` = ? limit ?', + bindings: [1, 2, 1], + }, + mssql: { + sql: 'select * from [users] where [id] = ? union all select * from [users] where [id] = ?', + bindings: [1, 2], + }, + pg: { + sql: 'select * from "users" where "id" = ? union all select * from "users" where "id" = ? limit ?', + bindings: [1, 2, 1], + }, + 'pg-redshift': { + sql: 'select * from "users" where "id" = ? union all select * from "users" where "id" = ? limit ?', + bindings: [1, 2, 1], + }, + }); }); it('multiple unions', () => {