iog-kc

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?

First 3 of 3 Posts Switch mode

fuelen

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)
iog-kc

iog-kc OP

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 translations child relationship. We are using a function to get both the child schema and the fields that need to be mapped with select_merge.

halostatue

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 as Map.take(t || %{}, fields) would in pure Elixir.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement