0

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?

16
  • might be a race effect, maybe you need to await certain functions before executing your query? Commented Oct 11, 2018 at 11:35
  • @HRK44 there is only one query running at the same time. Also there were no problems with other queries before, as I mentioned already Commented Oct 11, 2018 at 11:37
  • How was the table created? Commented Oct 11, 2018 at 11:40
  • 2
    @RaymondNijland okay, I found what the problem was. I was just stupid enough to lose space at the end of the first line. Sorry for bothering you Commented Oct 11, 2018 at 11:46
  • 1
    In general, it's always good to print the generated query to check for any syntax issues. Commented Oct 11, 2018 at 11:48

1 Answer 1

1

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;'
) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.