Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use a string in SELECT clause on knex js (postgres)

Hi I would like to include a string value to SELECT clause on KnexJS. It worked fine on Postgresql, but I am not sure how to convert that query into Knex syntax.

Here is Postgresql:

select date, 'UNFINISHED' from task_history
where store_id=1 and date=20160527

Here is Knex:

await db.table('task_history')
.count('*')
.select('date', knex.raw("UNFINISHED"))
.where({ 
     store_id: request.params.storeid,
     finish_time: null 
}) 
.whereBetween('date', [request.params.sdate, request.params.edate])                    
.groupBy('date')

Knex one gives me an error saying that there is no column called UNFINISHED. "UNFINISHED" is a string value that I would like to return with the query result.

I've tried to use knex.raw() as mentioned first solution below, but somehow, I could not use knex.raw(). Maybe that's because of 'await'? I am not sure.

Update: .select('date', db.raw("'UNFINISHED'")) Just a single quote missing.

like image 529
notentered Avatar asked May 27 '16 00:05

notentered


1 Answers

For literal strings use knex.raw()

db.table('task_history')
.select('date', db.raw("UNFINISHED"))
.where({store_id:1, date:20160527})
like image 111
EoinS Avatar answered Oct 23 '22 04:10

EoinS