coen.bakker
Dynamic sub-querying based on field in top-level query: looking for a better approach
I have a function get_by_id/1 that returns a database entry and does so correctly (see below). My approach certainly is naive and resulted in verbose code and probably also relatively bad performance.
def get_by_id(id) do
draft = Repo.get(Draft, id)
case draft.type do
"regular" ->
query =
Draft
|> join(:inner, [d], t in subquery(RegularDraft), on: t.draft_id == d.id)
|> select_merge([d, t], %{story: t.story})
|> preload([:authors, :publications])
Repo.get(query, id)
"story_starter" ->
query =
Draft
|> join(:inner, [d], t in subquery(StoryStarterDraft), on: t.draft_id == d.id)
|> select_merge([d, t], %{story: t.story, starter: t.starter})
|> preload([:authors, :publications])
Repo.get(query, id)
end
end
As you can see, the function first gets draft from the database and then uses a case ... do to dynamically perform a query that is returned from the function. It’s that dynamic subquery that throws me off. Is this where you would use Dynamic queries — Ecto v3.14.0? I have trouble translating the documentation to my use case. Below is my naive attempt at it (that does not work and throws an error).
def get_by_id(id) do
query =
Draft
|> join(:inner, [d], t in ^type_query(d.type), on: t.draft_id == d.id)
|> select_merge([d, t], %{story: t.story})
|> preload([:authors, :publications])
Repo.get(query, id)
end
def type_query("regular") do
dynamic([t], subquery(RegularDraft))
end
def type_query("story_starter") do
dynamic([t], subquery(StoryStarterDraft))
end
Most Liked
JohnnyCurran
It seems like you are switching the module / schema you query based on the draft type. In that case, something like the following could work
module = case draft.type do
"regular" -> RegularDraft
"story_starter" -> StoryStarterDraft
end
from(d in Draft,
join: t in subquery(module),
on: t.draft_id == d.id,
where: d.id == ^id,
select: %{story: t.story, starter: t.start}
)
|> Repo.one()
|> Repo.preload([:authors, :publications])
Un-tested, but should be close
JohnnyCurran
One option could be to use a UNION ALL. It’s slightly duplicative but it is a single query. The problem is I don’t see how to use the draft.type binding on the subquery before having retrieved it from the outer query. And we can’t know to query Regular or StoryStarter before we know the draft type.
This makes the assumption that a draft will only ever be one of a regular or story_starter. The Inner join will return zero rows on one of the queries, leaving you with the desired result:
def get_by_id(id)
starter_query =
from(d in Draft,
join: t in StoryStarterDraft,
on: t.draft_id == d.id,
where: d.id == ^id,
where: d.type == "story_starter",
select: %{
story: t.story,
starter: t.starter
}
)
query = from(d in Draft,
join: t in RegularDraft,
on: t.draft_id == d.id,
where: d.id == ^id,
where: d.type == "regular",
select: %{
story: t.story,
starter: nil # UNION ALL needs the result sets to be the same width
},
union_all: ^starter_query
)
Repo.one(query)
end
JohnnyCurran
You could combine these, too:
{type_module, type_fields} =
case type do
"regular" -> {RegularDraft, [:story]}
"story_starter" -> {StoryStarterDraft, [:story, :starter]}
end
# query
If you sometimes will know the type/id then making an extra (small) query isn’t the end of the world.
Popular in Questions
Other popular topics
Latest Phoenix Threads
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









