"DELETE FROM locations ORDER BY id DESC LIMIT" knex.js 上的 1 个不工作

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

我试着写这个命令:

DELETE FROM locations ORDER BY id DESC LIMIT 1

于 knex.js。 我写这段代码:

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

但是return

DELETE FROM locations

为什么?

因为 Postgres 不支持 LIMIT,而 Knex 应该与 MySQL 和 PostgreSQL 兼容。见 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.

那里提到它从 0.6 开始工作,但我无法验证,它似乎仍然无法工作,这似乎是一种设计选择。 (我个人觉得这很有问题,因为没有错误,你只是默默地把所有东西都删除了。)

处理 DELETE LIMIT 语句的唯一方法是使用 knex.raw 然后作为原始查询。