【问题标题】:Pgsql error: You might need to add explicit type castsPgsql 错误:您可能需要添加显式类型转换
【发布时间】:2017-06-28 05:13:57
【问题描述】:

我的网站运行良好,直到我将它部署到 heroku,问题是 heroku 使用 pgsql,而我使用的是 mysql 和 laravel 框架。

我的查询是

$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();

这是我将它部署到 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 %))

我尝试使用像 CAST(vaccine_id AS VARCHAR) 这样的强制转换,但我没有收到错误,但它没有返回任何结果。

【问题讨论】:

  • 我不太了解这个 API,但是where "patient_address" ILIKE %San Francisco 是错误的,San Francisco 应该用单引号 AFAIK。
  • 这是一个变量 $request->input = san fancisco 我如何在上面加上单引号?
  • 我不确定,只能告诉你,如果查询输出是逐字的,那就错了。

标签: postgresql heroku


【解决方案1】:

你可以这样做:

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

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

【讨论】:

  • 我在通过 JPA 调用过程时遇到了这个错误。尝试使用 :: 运算符进行转换但没有成功, cast() 没有任何问题。谢谢。
【解决方案2】:

问题出在这里:

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

看起来疫苗ID是整数,你不能使用运算符ILIKE来整数。试试'='

如果您想使用 LIKE、ILIKE 或其他文本运算符,您必须将数据转换为文本。在 SQL 中,它必须看起来像:

WHERE "vaccine_id"::text ILIKE val

改为

WHERE "vaccine_id" ILIKE val

【讨论】:

  • 您的错误是 operator does not exist: integer ~~* unknown - 这是因为疫苗 ID 是整数。并且整数没有运算符 ILIKE (~~)。
  • 因为mysql不是纯sql。至少对我来说。可能它通过某些规则默认转换数据。
  • 运算符 '=' 适用于所有类型,postgresql 可以转换第二种类型,但不能转换为第一种。即“age” = '2' - postgres 看到“age” 是 int 并将 '2' 转换为 int。但是第一个参数你必须手动转换
  • 不,因为 - 是一个 laravel 方法。可能类似于 $q->WhereRaw("vaccine_id::text ILIKE '%?%' , [$vaccine_id]")
  • 为什么需要 ilike?如果这个整数可以使用任何其他比较? ilike 是不区分大小写的,你用它做什么类型的检查?
猜你喜欢
  • 2015-04-07
  • 2019-05-27
  • 1970-01-01
  • 2020-06-16
  • 2013-09-09
  • 2017-07-16
  • 2021-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多