tjdam

tjdam

Hi!

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: g.id == ^g.id
  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?

Thanks!

Showing Posts 1 to 3

LostKobrakai

LostKobrakai

  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: g.id == ^id,
  preload: […]
tjdam

tjdam OP

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: g.id == ^id,
  preload: [...],
  select: %{name: u.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?

tjdam

tjdam OP

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: u.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?

— All posts loaded —

Where Next? Top

Trending in Questions Top

itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
mandar1045
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews