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
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’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
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
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
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
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
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
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
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











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.