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

.forUpdate() inserts "as" when passed an array of table names #4878

Closed
jbrawnjr opened this issue Dec 7, 2021 · 1 comment · Fixed by #4882
Closed

.forUpdate() inserts "as" when passed an array of table names #4878

jbrawnjr opened this issue Dec 7, 2021 · 1 comment · Fixed by #4882

Comments

@jbrawnjr
Copy link

jbrawnjr commented Dec 7, 2021

Environment

Knex version: 0.95.14
Database + version: PostgreSQL 12.7 (pg 8.7.1)
OS: AWS RDS

Bug

  1. Explain what kind of behaviour you are getting and how you think it should do

I'm using .forUpdate([]) with a list of table names. I would expect this:

.forUpdate(['table1', 'table2'])

to render this into the query:

FOR UPDATE OF "table1", "table2"
  1. Error message

The above is instead rendering this into the query:

FOR UPDATE OF "table1" AS "0", "table2" AS "1"

These AS additions are causing errors in SQL:

ERROR: syntax error at or near "AS"

According to the PostgreSQL docs, AS cannot occur here, which is why this throws an error. (I linked to v12, but it is the same in the current version as well.) Indeed, if I print the query out, remove the AS "0" and AS "1" and run the query directly on the DB, the query returns data successfully.

  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.
const Knex = require('knex');

const knexPg = Knex({
  client: 'pg',
});

(async function run() {
  const query = knexPg.queryBuilder()
    .select(['id'])
    .from('table1')
    .leftJoin('table2', 'table1.fk_id', 'table2.id')
    .forUpdate(['table1', 'table2']);
    
  console.log(query.toString());
})();

// Outputs the following:
// select "id" from "table1" left join "table2" on "table1"."fk_id" = "table2"."id" for update of "table1" as "0", "table2" as "1"
@kibertoad
Copy link
Collaborator

Released in 1.0.1.

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

Successfully merging a pull request may close this issue.

2 participants