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

knex splits on "as" in column name i.e. knex does not allow "as" in column name #6023

Open
tzeneva opened this issue Mar 6, 2024 · 1 comment

Comments

@tzeneva
Copy link

tzeneva commented Mar 6, 2024

Environment

Knex version: latest ( ^0.21.21 )

Bug

knex({ client: 'pg' })
  .select(knex.ref('Date as of'))
  .from('table')
  .toSQL().sql;

or

knex({ client: 'pg' })
  .select(knex.raw('??', 'Date as of'))
  .from('table');
  .toSQL().sql;

return

'select "Date" as "of" from "table"'

whereas I expect

'select "Date as of" from "table"'

Feature discussion / request

There are legitimate cases where a column name contains "as" (the above is from a real in-production example). Splitting on "as" is error-prone and leads to unexpected behavior. Instead, there should be a proper syntax for column aliasing.

This problem is also mentioned in this old issue: #139

@rluvaton
Copy link
Member

You can use: (single question mark)

knex({ client: 'pg' })
  .select(knex.raw('?', 'Date as of'))
  .from('table');
  .toSQL().sql;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants