adonisjs / lucid

AdonisJS SQL ORM. Supports PostgreSQL, MySQL, MSSQL, Redshift, SQLite and many more
https://lucid.adonisjs.com/
MIT License
1.09k stars 195 forks source link

useWhereRaw error "bind message supplies 2 parameters, but prepared statement "" requires 1" #1005

Closed gyu-dave closed 9 months ago

gyu-dave commented 9 months ago

Package version

20.1.0

Describe the bug

When using the whereRaw or orWhereRaw lucid methods to query a JSONB column, I receive the following error:

select count(*) as "total" from "items" where "type" = $1 and content::json->>'title' ilike '%$2%' - bind message supplies 2 parameters, but prepared statement "" requires 1

The query in question is either of the following:

.if(query?.filters?.title, (q) => {
        q.whereRaw("content::json->>'title' ilike '%:title%'", {
          title: query.filters.title,
        })
})
.if(query?.filters?.title, (q) => {
        q.whereRaw("content::json->>'title' ilike '%?%'", [query.filters.title])
})

~The underlying knex query works fine and will do as a work around~ It appears the knex query also doesn't work, so possibly not a lucid issue and may be knex or... myself

.if(query?.filters?.title, (q) => {
  q.knexQuery.whereRaw("content::json->>'title' ilike '%:title%'", {
    title: query.filters.title,
  })
})

I can attach a reproduction repo if needed, just need some time to get it together

Thanks a heap

Reproduction repo

No response

gyu-dave commented 9 months ago

Looks like it was a "me" issue and had incorrectly formatted SQL, so closing the ticket. Sorry to be a bother.

Here is the formatted solution that worked, for any one searching:

q.orWhereRaw("content::json->>'title' ilike '%' || ? || '%'", [query.filters.title])