I have this function that has an Ecto.Multi
operation where I include a Multi.insert
if the function argument (a map) field is not nil or empty.
review_changeset =
%Review{}
|> Review.changeset(attrs)
Multi.new()
|> Multi.insert(:review, review_changeset)
|> Multi.run(:review_photo, fn transaction_repo, %{review: %Review{id: review_id}} ->
case Map.fetch(attrs, "photo_url") do
{:ok, nil} ->
nil
{:ok, ""} ->
nil
{:ok, photo_url} ->
transaction_repo.insert(
ReviewPhoto.changeset(
%ReviewPhoto{},
%{review_id: review_id, photo_url: photo_url}
)
)
:error ->
nil
end
end)
|> Repo.transaction()
This seems to work if the photo_url has a value, but if it’s nil or empty (someone hasn’t attached a picture) it fails with the following error:
[error] GenServer #PID<0.781.0> terminating
** (RuntimeError) expected Ecto.Multi callback named `:review_photo` to return either {:ok, value} or {:error, value}, got: nil
(ecto 3.12.5) lib/ecto/multi.ex:905: Ecto.Multi.apply_operation/5
What am I missing here?