iog-kc
Select_merge with dynamic fields on a left outer join
We have a query that does a left outer join on a child table and then uses select_merge to merge dynamically selected fields from the child row into the parent. However, when there is no child row, the call to map fails with cannot merge because the right side is not a map, got: nil. Is there a way to get the call to map to use an empty map for merging when there isn’t a row to merge?
As a simplified example (assume that Product exists and has virtual fields for the translated fields in
Product.Translation), we have the function below where the translated_fields are provided contextually (for a product list, you might just want the name, but on a product display page, you want the description as well):
def product_with_translations(sku_code, locale, translated_fields \\ ~w(name)a) do
Product
|> from(as: :product)
|> where(sku_code: ^sku_code)
|> join(
:left, [product: p],
t in Product.Translation,
on: t.product_id == p.id and t.locale == ^locale,
as: :translation
)
|> select([product: p], p)
|> select_merge([translation: t], map(t, ^translated_fields))
end
As long as the associated Product.Translation exists, this works beautifully. However, if we were to ask for locale de-DE and don’t have it we receive an ArgumentError:
iex> Repo.one(product_with_translations("123", "fr-CA", ~w(name description)a))
** (ArgumentError) cannot merge because the right side is not a map, got: nil
Nominally, it seems that this should work, but how can we make map produce a default empty map on a left outer join? Or is this an Ecto bug or missing feature?
First Post!
fuelen
Are translated_fields really dynamic?
If no, then I’d recommend to use simpler select expression:
|> select([product: p, translation: t], %{p | name: coalesce(t.name, p.name)})
actually, multiple select_merge can be composed:
translated_fields
|> Enum.reduce(query, fn
# it would be great to have something like this:
# field, query ->
# query |> select_merge([product: p, translation:t], %{^field => coalesce(field(t, ^field), field(p, ^field))})
# but dynamic atoms are not supported as a field key
:name, query ->
query
|> select_merge([product: p, translation: t], %{name: coalesce(t.name, p.name)})
:description, query ->
query
|> select_merge([product: p, translation: t], %{description: coalesce(t.description, p.description)})
end)
Popular in Questions
Other popular topics
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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









