Preloading a join inside a from function


I have this somewhat convoluted query (for me at least) that fetches an User based on a given Group id he’s part of and if it has an assigned role in this Group. Like so:

from(g in Group,
  where: == ^
  inner_join: r in assoc(g, :user_group_relationships),
  on: not is_nil(r.role),
  inner_join: u in assoc(r, :user),
  select: u)

Then I end up with this list of Users by running the query through Repo.all, and call Repo.preload to preload a field in User schema.

I do that because I couldn’t figure out how to include this preload bit in that from function before.

I have two questions;

  1. Is doing that (running a query with Repo.all and then preloading) inefficient or does Ecto merge everything in one operation behind the scenes anyways?

  2. How to preload an User field in this case?


  1. No it does not. But doing two queries in not by by default.

  2. It’s likely simpler to reverse the query:

from u in User, 
  join: r in assoc(u, :user_group_relationships), 
  on: not is_nil(r.role),
  join: g in assoc(r, :group),
  where: == ^id,
  preload: […]
1 Like

Oh my god, hahaha :sweat_smile:

Yeah, I guess that makes A LITTLE (a lot) more sense, thank you!

But the thing is, I was trying to bring the preload to the from function because I wanted to create a custom map using select in the end of the query, and I thought I wouldn’t be able to preload it afterwards if the result of the query would not be a Ecto struct.

But if I go:

from u in User, 
  join: r in assoc(u, :user_group_relationships), 
  on: not is_nil(r.role),
  join: g in assoc(r, :group),
  where: == ^id,
  preload: [...],
  select: %{name:, role: r.role}

I get:

    ** (Ecto.QueryError) the binding used in `from` must be selected in `select` when using `preload` in query

I don’t quite understand what it means…

I know the preload works cause if I just use select: u it returns the list of User structs fine and if I select a tuple (i.e. select: {u, r.role}) I get the results I want, but then I have to map the result to create the new map for each element returned, which doesn’t seem right to me.

Is there a way to do that new map straight from select?

I realized it means I wasn’t including the preload in the select… obviously.

I don’t know if its unproductive to keep writing in this thread since my issues seem to be slightly moving away from the original question, but the issue I’m having now is that by using select: %{name:, preloaded_field: u.preloaded_field} I get an error saying the field I’m preloading is a “virtual field”.

Isn’t that always the case for preloads, though?

My reasoning is that because preload happens after the query I can’t use the result of a preload to form the result of my query, is that right?
In that case, I’d always have to query, preload and then map the results get a final result?