我在插值UUID列表以执行in
查询时遇到问题。
Ecto.Adapters.SQL.query!(Repo, "
WHERE
some_id IN ($1)
", [some_list_of_ids])
这给我一个错误** (ArgumentError) Postgrex expected a binary of 16 bytes, got ["4ca72ee7-61e2-4450-8dac-bdd7cf6b3df9", "83640dcc-9674-462c-881b-0ce2ed8f3fba",...
。即使我使用UUID.string_to_binary!
我该如何插值?
答案 0 :(得分:0)
尝试将“破折号” UUID转换为二进制UUID。
这应该有效:
uuids = [
"f3c106c3-943f-4270-b129-59a2fda48925",
"5ead56f1-9b7f-4289-9a8d-236c2812d82a",
"cf4b7ddc-1369-4d99-b35d-8dae6c9e85d9"
]
binary_uuids = Enum.map uuids, fn uuid -> Ecto.UUID.dump(uuid) |> elem(1) end
query = "select * from accounts where id = ANY($1)"
Ecto.Adapters.SQL.query!(Repo, query, [binary_uuids])