innocentoldguy
Hello, Elixir Forum,
I’m trying to build a query that joins four tables together and returns a map that looks like this:
IDEAL RESULT
%{
agent_domain: "Agent Domain",
agent_hostname: "Agent Hostname",
organization_name: "Organization Name",
site_name: "Site Name",
licenses: [
%{
...license keys and values.
},
%{
...license keys and values.
}
]
}
So far, I have this query working:
query =
from o in Organization,
join: s in Site,
join: a in Agent,
join: al in AgentLicense,
on:
o.id == s.organization_id and
s.id == a.site_id and
a.id == al.agent_id,
where: [id: ^id],
select: %{
organization_name: o.name,
site_name: s.name,
agent_domain_name: a.domain_name,
agent_hostname: a.hostname,
activation_licenses: al
}
Which returns a structure that looks like this:
ACTUAL RESULT
[
%{
license: %{
...license keys and values.
},
agent_domain: "Agent Domain",
agent_hostname: "Agent Hostname",
organization_name: "Organization Name",
site_name: "Site Name"
},
%{
license: %ActiphyPortal.Agents.AgentLicense{
...license keys and values.
},
agent_domain: "Agent Domain",
agent_hostname: "Agent Hostname",
organization_name: "Organization Name",
site_name: "Site Name"
}
]
The ACTUAL RESULT makes sense based on my query. My question is whether or not there is a way in Ecto to keep this as one query but have Ecto nest the result so that information isn’t duplicated, like the IDEAL RESULT structure above?
Thanks in advance for your help.
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
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 working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
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 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
The data returned by the db will always be a list of lists of column values. Ecto has some rudimentary means of mapping the list of columns to structs or maps, … and deduplicating things like assocs, but in your case being parts of the return values deduplicated and parts not (without a known assoc tree) is not possible without postprocessing results afterwards.
chulkilee
You can use
Repo.one/2to fetch single result (instead of a list) from a query.al2o3cr
Here’s what I get out of this query (if it doesn’t match your application, the advice that follows is probably wrong):
What it looks like your query is ultimately looking for is a result for each agent containing the agent’s licenses and some metadata about the agent. That query would look like:
(adjust
licensesabove if your association isn’t named that)This loads a little more data than strictly necessary from
organizationsandsitesbut will group the results like you’re looking for.chulkilee
Oh, I misread your post
What is the
id? From your ideal result, it seems like you want to query by agent id. If so, it’s better to iterate by agent_list, and join others and transform the result to your “ideal” result.Some notes
from.Here is the one query version - joining everything, taking duplicate common fields, and cleaning up with elixir
However, the above approach will consume more db connection bandwidth and client side memory, since the common part is being duplicated.
To avoid that, the simplest solution is to make two queries in a transaction - 1) agent & org with agent id, and 1) agent licenses with one query.
If you just need to access assoc from the record (e.g. agent/site/org from agent license) - then you can just use preload without join - then it ecto will preload them in separate query automatically.