question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Syntax errors when using union with first/limit in 1.0.4

See original GitHub issue

Environment

Knex version: 1.0.4 Database + version: PostgreSQL 13.4 OS: Debian

Bug

In 1.0.4 the behavior (order and wrapping) of union changed causing queries that previously worked to generate syntax errors.

One example is:

const results = await knex('accounts')
  .select('last_name')
  .unionAll(function () {
    this.select('last_name').from('accounts');
  })
  .first();
expect(results).to.eql({ last_name: 'User' });

Which in 1.0.4 gives error: select "last_name" from "accounts" limit $1 union all select "last_name" from "accounts" - syntax error at or near "union".

But in 1.0.3 it generates select "last_name" from "accounts" union all select "last_name" from "accounts" limit ?.

Another query that fails is

const results = await knex
  .unionAll(
    function () {
      this.select('last_name').from('accounts');
    },
    function () {
      this.select('last_name').from('accounts');
    },
    true
  )
  .first();
expect(results).to.eql({ last_name: 'User' });

with error: (select "last_name" from "accounts") union all (select "last_name" from "accounts") (limit $1) - syntax error at or near "("

I believe this change is introduced in https://github.com/knex/knex/pull/5030. Is this new behavior intended and we should rewrite our union queries?

This following is working, so it’s not that hard to update the queries but it seems like a breaking change?

const results = await knex
  .unionAll(
    function () {
      this.select('last_name').from('accounts');
    },
    function () {
      this.select('last_name').from('accounts');
    }
  )
  .first();

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
OlivierCavadenticommented, Mar 17, 2022

Sorry for the wait, and thanks for all examples guys. I have find how to fix it and I added a lot of unit tests but still need more tests. I will push tomorrow at the end of day.

2reactions
OlivierCavadenticommented, Mar 14, 2022

@fnatte @wa11a I will fix it as soon as possible (today or tomorrow).

Read more comments on GitHub >

github_iconTop Results From Across the Web

'FROM' syntax error involved with huge UNION clause
Try the below code, you aliased the table not the field. SELECT a.options, Count(*) FROM( SELECT TBL.Des1 AS options FROM TBL UNION ALL ......
Read more >
Discriminated Unions - F# | Microsoft Learn
Learn how to use F# discriminated unions. ... including valid and error cases; data that varies in type from one instance to another; ......
Read more >
Syntax Error - Using "Group By" with "Union All"
Basically, I just want to group the Item Names (U_ItemName) together and have their respective quantities (U_Cart) added together so the same items...
Read more >
Towards an Error-free UNION ALL | dbt Developer Blog
Union 'ing two or more tables with a long list of columns can be an error-prone chore, which dbt + dbt_utils abstracts away....
Read more >
C51: Error 200 (Left Side of '.' Requires Struct/Union)
x accepted some structure pointer operations without generating syntax errors. For example: The following code works fine in C51 V5.x. 1 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found