Search code examples
javascriptpostgresqlknex.js

Why does certain query fails?


I'm trying to run following query with 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;'
)  

Any other query goes well, but this one fails with following error:

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)

Table tigers (as you can see, tiger_id column exists here):tigers table

Results of same query made with pgAdmin (the same I expect knex query to return): query result

So what is the problem here? Is there any other way to get expected result?


Solution

  • The problem was me skipped space in the end of the first line, so the working query is:

    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;'
    )