I’m using Ecto 2 and want to make a function that would accept a query and a key and then use the key in select_merge. I’m getting the following error when doing that:
iex(21)> from(p in Person, select: %{}, select_merge: %{id: p.id}) |> Repo.all()
[debug] QUERY OK source="people" db=0.8ms
SELECT p0."id" FROM "people" AS p0 []
[%{id: 2774176}, %{id: 2774177}, %{id: 2774178}]
iex(22)> key = :id
:id
iex(23)> from(p in Person, select: %{}, select_merge: %{^key => p.id}) |> Repo.all()
[debug] QUERY ERROR source="people" db=3.0ms
SELECT $1, p0."id" FROM "people" AS p0 [:id]
** (Postgrex.Error) ERROR 42P18 (indeterminate_datatype): could not determine data type of parameter $1
(ecto) lib/ecto/adapters/sql.ex:431: Ecto.Adapters.SQL.execute_and_cache/7
(ecto) lib/ecto/repo/queryable.ex:133: Ecto.Repo.Queryable.execute/5
(ecto) lib/ecto/repo/queryable.ex:37: Ecto.Repo.Queryable.all/4
iex(23)> query = from(p in Person, select: %{}, select_merge: %{^key => p.id})
#Ecto.Query<from p in Dsrv.Main.Person, select: %{^:id => p.id}>
iex(24)> Repo.to_sql :all, query
{"SELECT $1, p0.\"id\" FROM \"people\" AS p0", [:id]}
Is this possible at all to use a variable as a key with select_merge?