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?