knex: pg: incorrect "delete + join" query since 0.95.8

Environment

Knex version: 0.95.8 Database + version: postgresql 12.7 OS: linux/debian 11

Bug

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

I get an incorrect delete postgres query, containing the table name in the delete statement:

delete "my_table" from "my_table" left join "users" on "users"."user_id" = "my_table"."user_id" where "my_table"."criteria" < 10

It should produce this code instead:

delete from "my_table" left join "users" on "users"."user_id" = "my_table"."user_id" where "my_table"."criteria" < 10
  1. Error message error from postgres:
syntax error at or near ""my_table""
  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')({
	client: 'pg',
	connection: { /* redacted */ }
});

const query = knex
	.select('xyz')
	.from('my_table')
	.leftJoin('users', 'users.user_id', 'my_table.user_id')
        .where('my_table.criteria', '<', 10)
	.delete();

console.log(query.toString());

this code produces the following query in 0.95.8:

delete "my_table" from "my_table" left join "users" on "users"."user_id" = "my_table"."user_id" where "my_table"."criteria" < 10

and in 0.95.7:

delete from "my_table" where "my_table"."criteria" < 10

The 0.95.7 result is not correct either, but at least it generates a valid PostgreSQL statement.

I’m not sure the way knex is used in this example is correct, but I found an actual use of knex like this one in my codebase.

I hope this information is relevant, and if not, sorry for the noise.

Best Regards

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 21 (2 by maintainers)

Commits related to this issue

Most upvoted comments

Released in 1.0.1.

@Egor-Koldasov Will you be able to add USING support for pg, btw? I think @elhigu blessed the approach, so it’s just a matter of implementation at this point.