Question

Using this query:

users = User.where('confirmed_at is NULL AND confirmation_sent_at <= DATE_SUB(NOW(), INTERVAL ? days)', 1)

In mysql goes okay but in Postgresql it fails with:

PG::SyntaxError: ERROR:  syntax error at or near "1"
    LINE 1: ...AND confirmation_sent_at <= DATE_SUB(NOW(), INTERVAL 1 day))
                                                                 ^
    : SELECT "users".* FROM "users"
      WHERE (confirmed_at is NULL AND confirmation_sent_at <= DATE_SUB(NOW(), INTERVAL 1 day))

I'm trying to understand but missing the context here. Why is the integer 1 not valid in this query?

Was it helpful?

Solution

There is no function DATE_SUB() in PostgreSQL, so it cannot work.

This expression would work in Postgres:

... AND confirmation_sent_at <= (now() - interval '1 day')

Or, if confirmation_sent_at is a date:

... AND confirmation_sent_at <= (now()::date - 1)

OTHER TIPS

Try...

users = User.where(
    "confirmed_at IS NULL " +
    "AND confirmation_sent_at <= (NOW() - INTERVAL '1 DAY')"
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top