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.

Wrong quouting of expression

See original GitHub issue

Environment

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 😃 )

  1. 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?

  1. Error message

There’s no error message returned from knex itself but by the db when it attempts to execute the resulting query

  1. 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:open
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
madtrickcommented, Mar 7, 2022

Ah, nevermind, figured it out:

K.from('foo').leftJoin('bar', function () { this.on('foo.id', '=', 'bar.foo_id' ).andOn(K.raw('"foo.foo" || \'#\' || "foo.bar"'), K.raw(`'value-foo#value-bar'`)) }).toString()

yields

select * from "foo" left join "bar" on "foo"."id" = "bar"."foo_id" and "foo.foo" || '#' || "foo.bar" = 'value-foo#value-bar'
0reactions
maximelkincommented, Mar 8, 2022

K.raw(‘value-foo#value-bar’)

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 ??).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Counterspeech Doctrine | The First Amendment Encyclopedia
The counterspeech doctrine, first articulated by Louis Brandeis in First Amendment jurisprudence in 1927, posits that the remedy for false speech is more ......
Read more >
Quoting out of context - Wikipedia
Quoting out of context is an informal fallacy in which a passage is removed from its surrounding matter in such a way as...
Read more >
Scare Quotes : Quotations - University of Sussex
The use of quotation marks can be extended to cases which are not exactly direct quotations. Here is an example: ... The phrase...
Read more >
Quoting and Paraphrasing - UW-Madison Writing Center
Use single quotation marks for the embedded quotation: According to Hertzberg (2002), Dahl gives the U. S. Constitution “bad marks in 'democratic fairness'...
Read more >
What does (sic) mean? | Notes and Queries | guardian.co.uk
Sic is Latin for So or Thus. It is used to denote that a grammatical error, mistake or specific formating in a quoted...
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