为什么某些查询失败?

时间:2018-10-11 11:31:51

标签: javascript postgresql knex.js

我正在尝试使用knex.js运行以下查询:

database.raw(
    'SELECT DISTINCT ON(tiger_id) tiger_id, created_at, latitude, longitude, id' +
    'FROM images ' +
    'ORDER BY tiger_id, created_at DESC;'
)  

其他任何查询都可以顺利进行,但是此查询失败,并显示以下错误:

error: column "tiger_id" does not exist
at Connection.parseE (/node_modules/pg/lib/connection.js:553:11)
at Connection.parseMessage (/node_modules/pg/lib/connection.js:378:19)
at Socket.<anonymous> (/node_modules/pg/lib/connection.js:119:22)
at Socket.emit (events.js:182:13)
at Socket.EventEmitter.emit (domain.js:442:20)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at Socket.Readable.push (_stream_readable.js:219:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)

tigers(如您所见,此处有tiger_id列):tigers table

与pgAdmin进行的相同查询的结果(我希望与knex查询返回的结果相同):query result

那么这里是什么问题?还有其他方法可以得到预期的结果吗?

1 个答案:

答案 0 :(得分:1)

问题是我在第一行末尾跳过了空格,因此有效的查询是:

database.raw(
    'SELECT DISTINCT ON(tiger_id) tiger_id, created_at, latitude, longitude, id ' +
    'FROM images ' +                                               // this one ^
    'ORDER BY tiger_id, created_at DESC;'
)