iog-kc
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?
Trending in Questions
Other Trending 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
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 3 of 3 Posts
fuelen
Are
translated_fieldsreally dynamic?If no, then I’d recommend to use simpler select expression:
actually, multiple
select_mergecan be composed:iog-kc
Yes, the fields are dynamic. While the example I provided does use a single table, we are using this with multiple tables that all have a
translationschild relationship. We are using a function to get both the child schema and the fields that need to be mapped withselect_merge.halostatue
I have opened this as a bug in the Ecto tracker #3218 as the more I think about this,
map(t, ^fields)should probably act the same asMap.take(t || %{}, fields)would in pure Elixir.