abdelaz3r
Hello.
I have this Ecto Shema:
Instance -> has_many(Faction) -> many_to_many(Profils)
So, instances has many factions, and profils can register into a faction (given an instance).
I would like, when I load instances, to preload their corresponding factions (easy) and to add, for each faction, a field that is the number of registered profil.
First question: I dont know if I need to add a field (virtual?) to the faction schema (such as “registered_profils”).
Second question: How would look like the request?
For now I have this request that works (but it only preload factions into instances and profils into factions):
query =
from instance in Instance,
left_join: factions in assoc(instance, :factions),
left_join: profils in assoc(factions, :profils),
preload: [factions: {factions, profils: profils}],
order_by: [desc: instance.id]
I also tried different queries such as:
query =
from instance in Instance,
left_join: factions in assoc(instance, :factions),
left_join: profils in assoc(factions, :profils),
preload: [factions: {factions, profils: profils}],
group_by: factions.id,
select: {factions.registered_profils, count(profils.id)},
order_by: [desc: instance.id]
But none works for now. Any idea?
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
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 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
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
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
You can look at count distinct.
abdelaz3r
I can’t find documentation about count distinct with ecto.
I would know how to do that with plain SQL, but I’m quite new with ecto and I’m not sure what is the proper manner to do.
Kurisu
I did something similar sometime ago.
I used a virtual field for the count then make a querry like:
So your query could look like:
But to be honest I don’t know if this is the best way but it works. ^^
abdelaz3r
Ok, thanks a lot. It doesn’t work, but I think I undestand better
Now, the query looks like that:
And the real SQL query that ecto make is (after formatting):
Note: I have plugins for custom filter and pagination, so the limit and the where clause come from that.
And I have this error :
(Postgrex.Error) ERROR 42803 (grouping_error) column "i0.id" must appear in the GROUP BY clause or be used in an aggregate function.I’m not sure if it’s the query that is not correct or if I need some subqueries to achieve that.
Note2: I changed a bit the schema since my first post. Instead of having a many_to_many relation between Factions and Profils, I created a real schema “registration” that belong_to Factions and Profils (I need to store other informations into registration).
Kurisu
As the error said now the issue comes from the
group_byclause.You can use it only on primary id or in aggregate function. Try to refine the query then.
Good luck. ^^
Kurisu
If that can help you here is my own working query.
section has many categories
category has many ads
abdelaz3r
Yep ok, but the thing is, you don’t load data from section, you only filter category by the section_id. I’m trying to list all sections (in your situation), with their corresponding categories AND the count of ads. Which is why Postegres is not very happy.
But thanks again for the help
I’m now looking on this topic : Ecto subqueries with virtual fields? - #9 by xadhoom which may help me eventually.
abdelaz3r
I’m now with that sort of query`(doesn’t work either):
I’m trying to figure out if there is a way to assign the result of a select_merge to preload’s members (the virtual field registration_number is on the faction schema.
Kurisu
Ok in fact I also did that…
Here is my code:
But this means making 2 requests on the database.