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’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
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
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security











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