Skip to content

Commit

Permalink
Fix Union all + first syntax error (#4799)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierCavadenti committed Nov 3, 2021
1 parent 90b6b18 commit 75ece73
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/query/querycompiler.js
Expand Up @@ -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() {
Expand Down
30 changes: 30 additions & 0 deletions test/unit/query/builder.js
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 75ece73

Please sign in to comment.