Select data from two tables but return selected fields dynamically inside map

I have a query which successfully joins table with correct condition but its not returning the joinee table selected data in a map. Is there a way I can get data inside map like

from t1 in "Table1",
   join: t2 in "Table2", on: t1.t2_id == t2.id,
   select: map(t1, [:id, {"Table2", [:id]}])

Should return something like[%{"id": 1, "Table2": %{"id": 1}}] when above query runs but it only returns[%{"id": 1}]

But if I run this query

from t1 in "Table1",
    join: t2 in "Table2", on: t1.t2_id == t2.id,
    select: %{id: t1.id, Table2: %{id: t2.id}}

I would loose the ability to dynamically select fields

I haven’t tried this specifically, but I recently learned about select_merge. Perhaps something like this will work, though it doesn’t create a nested map.

from t1 in "Table1",
   join: t2 in "Table2", on: t1.t2_id == t2.id,
   select: map(t1, [:id]),
   select_merge: map(t2, [:id])
3 Likes