Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pgsql error: You might need to add explicit type casts

Tags:

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.

like image 545
Christian Avatar asked Feb 10 '17 00:02

Christian


People also ask

What is explicit typecast in SQL?

Explicit conversions use the CAST or CONVERT functions. The CAST and CONVERT functions convert a value (a local variable, a column, or another expression) from one data type to another. For example, the following CAST function converts the numeric value of $157.27 into a character string of '157.27' : SQL Copy.

How do I write a cast in PostgreSQL?

PostgreSQL supports a CAST operator that is used to convert a value of one type to another. Syntax: CAST ( expression AS target_type ); Let's analyze the above syntax: First, specify an expression that can be a constant, a table column, an expression that evaluates to a value.

How do I assign a variable in PostgreSQL?

Variables can be assigned default values within the declaration section of a PL/pgSQL code block. This is known as default value assignment, and is done by using the assignment operator (:=) on the same line as the variable's declaration.


1 Answers

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 
like image 139
Roman Tkachuk Avatar answered Sep 18 '22 19:09

Roman Tkachuk