Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Union all + first syntax error (#4364) #4799

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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