[英]Rails -PG::InvalidTextRepresentation: ERROR: malformed array literal
add_column :ssr_service_markets, :origin, :string, array: true, default: []
and when i want to 当我想
SSRService::Market.where(origin: "*", destination: "*").first
I have got 我有
PG::InvalidTextRepresentation: ERROR: malformed array literal: "*" LINE 1: ...service_markets" WHERE "ssr_service_markets"."origin" = '*' DETAIL: Array value must start with "{" or dimension information
How to fix it? 如何解决?
In Postgres, to check if an array contains given element, you can use @>
array operator. 在Postgres中,要检查数组是否包含给定元素,可以使用@>
数组运算符。
Read more: Array Operators 阅读更多: 数组运算符
:origin
is an array field in your ssr_service_markets
table, which means that it can contain multiple values. :origin
是ssr_service_markets
表中的一个数组字段,这意味着它可以包含多个值。
In your example, assuming that destination
is an array field as well, you could try to do the searching this way: 在您的示例中,假设destination
也是数组字段,则可以尝试通过以下方式进行搜索:
Model.where(["origin @> ? AND destination @> ?", '{*}', '{*}')"])
Remember about using curly braces when working with array values . 记住在处理数组值时要使用花括号 。
To write an array value as a literal constant, enclose the element values within curly braces [...] 要将数组值写为文字常量,请将元素值括在花括号内[...]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.