28

My website is just working fine til i deployed it to heroku and the problem is heroku uses pgsql and I'm using mysql and laravel framework.

my query is

$patient = Patient::where('patient_address', 'ILIKE' ,'%' . $request->input)->where('patient_sex', 'ILIKE' ,'%' . $request->gender)->whereHas('users', function($q) use($vaccine_id){
        $q->where('vaccine_id','ILIKE','%' . $vaccine_id);
    })->get();

here's what I'm getting when I deploy it to heroku

SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: integer ~~* unknown LINE 1: ...ient_id" = "patients"."PatientID" and "vaccine_id" ILIKE $3)

HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. (SQL: select * from "patients" where "patient_address" ILIKE %San Francisco and "patient_sex" ILIKE % and exists (select * from "vaccines" inner join "immunizations" on "vaccines"."VaccineID" = "immunizations"."vaccine_id" where "immunizations"."patient_id" = "patients"."PatientID" and "vaccine_id" ILIKE %))

I have tried using cast like CAST(vaccine_id AS VARCHAR) and I' not getting the error but it doesnt return any result.

3
  • I don't know this API well, but where "patient_address" ILIKE %San Francisco is wrong, San Francisco should be in single quotes AFAIK. Feb 10, 2017 at 0:35
  • it's a variable $request->input = san fancisco how do i put single quote on it?
    – Christian
    Feb 10, 2017 at 0:47
  • I'm not sure, only I can tell you that if the query output is verbatim, then it's wrong. Feb 10, 2017 at 0:47

2 Answers 2

32

The problem is here:

$q->where('vaccine_id','ILIKE','%' . $vaccine_id)

looks like vaccine_id is integer, and you can not use operator ILIKE to integer. Try just '='

If you want to use LIKE, ILIKE or other text operator you must cast your data to text. In SQL it must looks like:

WHERE "vaccine_id"::text ILIKE val

instead

WHERE "vaccine_id" ILIKE val
12
  • 1
    Your error is operator does not exist: integer ~~* unknown - this is because vaccine_id is integer. And integer does not have operator ILIKE (~~). Feb 11, 2017 at 11:29
  • 1
    Because mysql is not pure sql. At least as for me. Probably it cast by default data by some rules. Feb 11, 2017 at 11:35
  • 1
    operator '=' exists for all types, postgresql can cast second type, but not first. i.e. "age" = '2' - postgres saw than "age" is int and cast '2' to int. But first argument you must cast manually Feb 11, 2017 at 11:55
  • 1
    No, because where - is a laravel method. Probably something like $q->WhereRaw("vaccine_id::text ILIKE '%?%' , [$vaccine_id]") Feb 11, 2017 at 12:02
  • 1
    Why do you need ilike? If this integer may be use any other compare? ilike is case insesivity like and for what type cheking do you use it? Feb 11, 2017 at 12:39
8

You could do this:

$q->where('cast(vaccine_id AS VARCHAR)','LIKE','%' . $vaccine_id)

OR

$q->where('cast(vaccine_id AS TEXT)','LIKE','%' . $vaccine_id)
1
  • I was facing this error while calling a procedure through JPA. Tried to cast with :: operator but didn't work, cast() worked without any issues. Thank you.
    – sarath
    Jun 7, 2021 at 3:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.