lorenzomar
Hi folks,
I have a sample application with User and Comments schemas with a relation 1:m between them.
I would like to add a 1:1 relation on User schema to retrieve last approved comment.
Do you know if there is some sort of preload_order option in has_one as we have in has_many relation?
Thanks in advance!!
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
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
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
victorbjorklund
Yes, you can just do it the same way as for has_many.
So for a schema like this:
You can achieve it like this:
lorenzomar
Sorry what I mean was to have both
whereandorder_bytogether when the association is defined.Something like:
In
has_manyexistspreload_orderoption (Ecto.Schema — Ecto v3.14.0) that can be used to sort the related recordsjerdew
That sort of dynamic relationship isn’t how has_one is meant to work. You have to manually control a has_one such as: When you add a new comment, you would unset the current latest_comment and then set the new entry as the latest_comment.
What you’re thinking would take the shape of a
virtual: truefield, and you’d define a query like above. When you load the struct, you’d then run a separate query for the latest_comment, and populate the virtual field.victorbjorklund
Aha, I see. No that doesn’t seem possible. You could use my solution (even if it less clean) or a virtual field like @jerdew mentions.
benwilson512
Hey @lorenzomar the general challenge with this is when you are dealing with a collection of users and you want to get the most recent approved comment for each of them. This is doable with a lateral join, but at least last I looked Ecto won’t do this for you.
What will happen in the case of a
has_onewith apreload_orderis that it will load ALL comments and then get the first one in memory. This is going to be a foot gun when you get more records.saveman71
Hi @benwilson512, I’m not sure why would a (theoretical at the moment)
preload_orderimplementation load all the records in memory, since it already has alimit 1in the select, did I miss something?I opened a proposal PR for an example implementation, which worked for our use case : feat: add preload_order for has_one by saveman71 · Pull Request #4505 · elixir-ecto/ecto · GitHub
While I agree that if you need this feature, your schema is probably flawed, but as it was basically free to implement I hope to see it land in main.
fteschke
From what I see in the Ecto debug log output, the standard
preloadquery does not include alimit 1.My guess: Because it is used both to preload for a single item and to preload for a list of items:
For this reason, I think this suggestion is broken, as it selects only 1 comment overall, instead of one comment per user:
(Maybe it used to work, but the Ecto internals have since changed?)
Instead of a
limit 1it should bedistinctonuser_id: