Wrong quouting of expression
See original GitHub issueEnvironment
Knex version: 1.0.1 Database + version: Postgres 11 OS: macOS
Bug
(I think this is a bug but please redirect me to elsewhere if necessary 😃 )
- Explain what kind of behaviour you are getting and how you think it should do
In one of our queries we want to use an expression that’s the concatenation of two columns as a where
restricition. Something like the following:
SELECT *
FROM foo
WHERE
foo.foo || '#' || foo.bar = 'value-foo#value-bar'
After several attempts we haven’t been able to get knex to properly quoute the expression which results in an error when executing the resulting SQL. For example:
knex.from('foo').where({'foo.foo || \'#\' || foo.bar': 'value-foo#value-bar'}).toString()
=>
`select * from "foo" where "foo"."foo || '#' || foo"."bar" = 'value-foo#value-bar'
Notice how the qouting of "foo"."foo || '#' || foo"."bar"
is incorrect (looks like "foo || '#' || foo"
is treated as one field/column). We are aware of the wrapIdentifier
option but that’d would a global setting that would require handling of lots of case just for one particular case.
Are we doing something wrong or is this a bug? Is there any workaround?
- Error message
There’s no error message returned from knex itself but by the db when it attempts to execute the resulting query
- Reduced test code, for example in https://npm.runkit.com/knex or if it needs real database connection to MySQL or PostgreSQL, then single file example which initializes needed data and demonstrates the problem.
knex.from('foo').where({'foo.foo || \'#\' || foo.bar': 'value-foo#value-bar'}).toString()
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
Top GitHub Comments
Ah, nevermind, figured it out:
yields
You can actually pass whole string, it will be wrapped anyway.
Also check this out: docs about knex.raw. This could help to avoid unsafe string interpolation (i.e. by using
??
).