I have a query in my application that is supposed to return all the surveys due today or earlier for which notifications have not been sent. The query looks like this:
select "surveys".* from "surveys"
where "reminder_count" = 0 and
trunc("date") <= to_date(CURRENT_DATE, 'YY-MM-DD')
And here's the data in the table:
| id | user_id | token | date | reminder_count |
|----|---------|----------------------------------|----------|----------------|
| 1 | 1 | 8c01a36d33a18a1ee9e0e6d26aa90eed | 18-06-18 | 0 |
| 2 | 1 | d608650acea377312eb18aea1e14688a | 18-07-16 | 0 |
As you can see, the first entry is dated yesterday and the second is a few weeks ahead. Therefore the query should only return the first row. And it does exactly so when I execute it directly in SQL Developer.
However, when I use it in my code (Javascript with Objection.js and Knex), it ignores the date clause and returns both rows.
Here's the code I use:
const surveysDue = await Survey.query()
.where({reminderCount: 0})
.where(raw('trunc("date")'), '<=', raw('to_date(CURRENT_DATE, \'YY-MM-DD\')'))
And here's what the debug output shows for this query:
{ method: 'select',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [ 0 ],
__knexQueryUid: '2a22ccb5-4763-42d3-bf0a-6ea95a7b9c7e',
sql:
'select "surveys".* from "surveys" where "reminder_count" = ? and trunc("date") <= to_date(CURRENT_DATE, \'YY-MM-DD\')' }
The date clause evidently is in the query, which only adds to the question why doesn't it work. Could you, please, point me in the right direction as to where to look for resolution? Or maybe someone has already encountered an issue like that?
Thank you.