Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"DELETE FROM locations ORDER BY id DESC LIMIT" 1 on knex.js not working

I try write this command:

DELETE FROM locations ORDER BY id DESC LIMIT 1

on knex.js. I write this code:

knex('locations').delete().orderBy('id', 'desc').limit(1).toString()

But it return

DELETE FROM locations

Why?

like image 263
PC Tea Avatar asked Dec 04 '25 04:12

PC Tea


1 Answers

Because LIMIT is not supported by Postgres and Knex is supposed to be compatible with both MySQL and PostgreSQL. See GitHub issue:

[...] this syntax isn't supported in Postgres, so that might be a bit unexpected if it's possible in one database and not another [...]

Please open a new feature request if you need this functionality to be changed.

It was mentioned there that it works since 0.6 but I can't verify that, it seems still to be not working, and that seems to be a design choice. (Personally I find this quite problematic because there is no error, you just silently get everything deleted.)

The only way to handle a DELETE LIMIT statement would be as raw query with knex.raw then.

like image 78
CherryDT Avatar answered Dec 05 '25 19:12

CherryDT