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.

Join or clause can't compare to constants

See original GitHub issue

Currently, it doesn’t seem to be possible to generate a query like the following (in MySQL):

select * from `users` inner join `contacts` on `users`.`id` = `contacts`.`id` and `users`.`name` = 'mungo'

My test looks approximately like this. Basically, the on clause doesn’t work with the richness of the where clause, and it probably should. Of course, with integer values JS can tell the difference between strings and identifiers, but not for strings, so it’s escaping a string value as an identifier in all cases.

This is needed because for left and right joins, you can’t move the on clause to the where clause.

    it("complex join with string value", function() {
      testsql(qb().select('*').from('users').join('contacts', function(qb) {
        qb.on('users.id', '=', 'contacts.id').andOn('users.name', 'mungo');
      }), {
        mysql: {
          sql: 'select * from `users` inner join `contacts` on `users`.`id` = `contacts`.`id` or `users`.`name` = \'mungo\'',
          bindings: []
        },
        default: {
          sql: 'select * from "users" inner join "contacts" on "users"."id" = "contacts"."id" or "users"."name" = \'mungo\'',
          bindings: []
        }
      });
    });

I’ll probably work on a patch/pull request later.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
bendruckercommented, Jan 15, 2015

Important change I realized was needed while testing. knex.raw(true) is fine, but you can’t do the same for a string since it won’t be escaped. So the documented recommendation is to use a binding:

this.on('pledges.id', 'payments.pledge_id')
      .on('payments.paid', knex.raw('?', [true]));

I know this looks gross but I can’t think of a better way to address it.

0reactions
OlivierCavadenticommented, Nov 9, 2021

As I can read, we always need to make something, maybe I can reopen it if anyone want to make a PR ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Join or clause can't compare to constants · Issue #626 - GitHub
My test looks approximately like this. Basically, the on clause doesn't work with the richness of the where clause, and it probably should....
Read more >
The type of one of the expressions in the join clause is ...
I'm getting this error: The type of one of the expressions in the join clause is incorrect. Type inference failed in the call...
Read more >
Avoid using constants in an ORDER BY clause | Redgate
Phil Factor explains why an ORDER BY clause should always specify the sort columns using their names, or aliases, rather than using an ......
Read more >
Cant use a condition comparing 2 variables
One workaround is to call Power Automate to validate the condition using two variables and return the response to PVA.
Read more >
Chapter 5. EPL Reference: Clauses - Index of / - EsperTech
Variables can be used in an expression anywhere in a statement as well as in the output clause for dynamic control of output...
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