Home > Back-end >  "DELETE FROM locations ORDER BY id DESC LIMIT" 1 on knex.js not working
"DELETE FROM locations ORDER BY id DESC LIMIT" 1 on knex.js not working

Time:05-31

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?

CodePudding user response:

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.

  • Related