Raw sql query into ecto schema

Question: How to load result of a raw sql query into regular ecto schema?
I have something that does not fit into abstractions ecto provides but sql output is essentially equal to regular “select * from tablename”. I think that’s pretty typical scenario for more complex projects and no, fragment() is not enough.
Old Ecto 2 version did use folowing code:

Ecto.Schema.__load__(model, nil, nil, nil, fields,
                           &Ecto.Type.adapter_load(__adapter__(), &1, &2))

But it obviously doesn’t work anymore since ecto internal structure changed, no more escape hatch

https://github.com/elixir-ecto/ecto/blob/v3.2/lib/ecto/repo/schema.ex gives ideas how does loading works but using it means copying and re-implementing half of library’s guts, to be broken on next update or so.

Did i miss something?

I believe Repo.load/2 is your friend.

4 Likes

Thank you! Feeling stupid for missing something this obvious