alaister
Hi,
I have the following ecto query:
post_query = from p in Post, where: p.id == ^id, join: c in assoc(p, :comments), join: u in assoc(c, :user), preload: [comments: {c, :user}]
How do I select the following fields: p.title, p.content, p.comments, c.comment, c.inserted_at and u.name?
I’m having trouble with this because of the wired joins I’m doing (Still learning). Also this query actually runs two queries on the database. Is there any way to do it all in one?
Thanks,
Alaister
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Molly101
You can use a pattern matching here to get only those fields You’re interested in, i.e.
%{title: title, content:content, comments: comments, user: %{name: name}} = Repo.get!(post_query)and then You can passcommentsthoughEnum.Map. In Phoenix app You can also use View - “show.json” approach to convert result of Your ECTO query to a Map with the fields that You need… Because anyway You can’t just encode ECTO query result to JSON…Linuus
This should make only one DB query.
Regarding selecting association fields, you can do something like this (not tested)
Not sure exactly what you want to get back but that’s one way to select fields
alaister
Thank you @Linuus! I was missing the
user: uon the end there. Still not entirely sure what that is doing?Also the data structure I’m trying to achieve looks like the following:
The problem I’m having with this structure is I’m not sure how to deal with the list of comments with the nested users.
Thanks again!
Linuus
The
user: utells ecto to use that join thingy for the preload and not issue another request to fetch that association. (I think… I’m quite new to Ecto myself.)So this query:
returns that structure, right? So what you want to do now is limit the number of selected fields for performance reasons?
alaister
That is completely correct!
Linuus
I had a discussion about this on IRC and it seems to often be better to not join manually and just preload the data (and let it do multiple queries).
This would issue one request to the DB:
But it would return a lot of excessive data because the join would cause the
Postdata to be sent for each comment (due to how SQL works). Ecto hides this though by throwing away the excessive data.You can se this if you run something like the query I wrote before:
It will return one struct for each Comment so if a Post has two comments:
You don’t see this behaviour when running without the select statement because Ecto takes care of mapping the data.
When just preloading:
This would issue three requests but no excessive data so in many cases it is actually more efficient (Ecto even does the requests in parallell when possible).
For example, if the same user has made all the comments, the user would only be fetched once. When joining, the user would be returned for each comment.
So, unless you know you have a bottleneck here it may not be worth it…
(I’m quite new to Ecto, so if someone sees something wrong here, please let me know!
)
alaister
Ahh okay that makes a lot of sense! Thanks again
My only question now is a much more general Ecto question and that’s how to order the comments in descending order. I understand that it has something to do with
order_by: [desc: c.inserted_at]but I’m not sure where to put this!Thanks,
Alaister
alaister
I think I have solved this one:
Also would selecting only the fields I needed make my queries more performant? If so how might I do it with the preloaded users without the join?
Thanks,
Alaister
shahryarjb
Hello, I have a problem when I want to use select in preload, please see this code
and error:
I should use select on
UserSchema, how can I fix this ?benwilson512