将UUID列表插值到原始SQL查询中(不能使用片段)

时间:2019-06-19 01:33:39

标签: elixir ecto

我在插值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!

将UUID转换为二进制,我仍然会收到此错误

我该如何插值?

1 个答案:

答案 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])