数据库分片查询错误"parameters must be of length 0 for query"
Database fragment query error "parameters must be of length 0 for query"
我正在尝试查询 building
table、jsonb metadata
列和 google_place_ids
中的 json 数组属性,这是一个数组。
Elixir 给我一个错误:
这是片段生成的查询:
SELECT b0."id", b0."base_sku", b0."name", b0."logo", b0."email", b0."active", b0."sync", b0."building_group_id", b0."operating_region_id", b0."building_package_id", b0."metadata", b0."location", b0."inserted_at", b0."updated_at" FROM "buildings" AS b0 WHERE (b0."metadata"->'google_place_ids' @> '[""]') AND (b0."active" = TRUE) ["1234231223123"]
代码如下:
defp query_by_google_place_id(query, google_place_id) do
from(b in query,
where: fragment("?->'google_place_ids' @> '[\"?\"]'", b.metadata, ^google_place_id),
where: b.active == true,
limit: 1)
end
这里是错误:
[error] #PID<0.1340.0> running Proxy.InstrumentedPlug terminated
Server: localhost:4000 (http)
Request: GET /something/google_place_id/1234231223123
** (exit) an exception was raised:
** (ArgumentError) parameters must be of length 0 for query %Postgrex.Query{columns:
这不是 PostgreSQL 理解的方式。问题是 Ecto 的 fragment
对你可以在那里使用的 SQL(或其他查询语言)一无所知,所以它结束了将 ?
视为查询参数,而 PostgreSQL 将其视为原始字符串 ""
。您需要做的是直接以以下形式传递字符串:
fragment("?->'google_place_ids' @> ?", b.metadata, ^"[\"#{google_place_id}\"]")
这行得通,少了引号,没有括号
defp query_by_google_place_id(query, google_place_id) do
from(b in query,
where: fragment("?->'google_place_ids' @> ?", b.metadata, ^google_place_id),
where: b.active == true,
limit: 1)
end
我正在尝试查询 building
table、jsonb metadata
列和 google_place_ids
中的 json 数组属性,这是一个数组。
Elixir 给我一个错误:
这是片段生成的查询:
SELECT b0."id", b0."base_sku", b0."name", b0."logo", b0."email", b0."active", b0."sync", b0."building_group_id", b0."operating_region_id", b0."building_package_id", b0."metadata", b0."location", b0."inserted_at", b0."updated_at" FROM "buildings" AS b0 WHERE (b0."metadata"->'google_place_ids' @> '[""]') AND (b0."active" = TRUE) ["1234231223123"]
代码如下:
defp query_by_google_place_id(query, google_place_id) do
from(b in query,
where: fragment("?->'google_place_ids' @> '[\"?\"]'", b.metadata, ^google_place_id),
where: b.active == true,
limit: 1)
end
这里是错误:
[error] #PID<0.1340.0> running Proxy.InstrumentedPlug terminated
Server: localhost:4000 (http)
Request: GET /something/google_place_id/1234231223123
** (exit) an exception was raised:
** (ArgumentError) parameters must be of length 0 for query %Postgrex.Query{columns:
这不是 PostgreSQL 理解的方式。问题是 Ecto 的 fragment
对你可以在那里使用的 SQL(或其他查询语言)一无所知,所以它结束了将 ?
视为查询参数,而 PostgreSQL 将其视为原始字符串 ""
。您需要做的是直接以以下形式传递字符串:
fragment("?->'google_place_ids' @> ?", b.metadata, ^"[\"#{google_place_id}\"]")
这行得通,少了引号,没有括号
defp query_by_google_place_id(query, google_place_id) do
from(b in query,
where: fragment("?->'google_place_ids' @> ?", b.metadata, ^google_place_id),
where: b.active == true,
limit: 1)
end