acrolink

acrolink

I am trying to load a book, update it and update the associated record like this:

Multi.new()
|> Multi.run(:get_book, fn _ -> Mango.Books.get_book!(line.book_id) end)
|> Ecto.Multi.run(:update_book, fn %{book: book} ->
  Books.update_book(Books.get_book!(book.book_id), %{status: 1})
end)
|> Ecto.Multi.run(:update_associated_record, fn %{book: book} ->
  Records.update_record(Records.get_record!(book.record_id), %{
    returned_at: :os.system_time(:seconds)
  })
end)
|> Repo.transaction()
|> case do
  {:ok, result} -> result
  {:ok, %{book: book}} -> book
  {:ok, %{record: record}} -> record |> whitelist_record_fields
end

This produces this error message:

** (exit) an exception was raised:
    ** (CaseClauseError) no case clause matching: %Mango.Books.Book{__meta__: #Ecto.Schema.Metadata<:loaded, "books">, author: "Waseem", code: 1116, id: 107, inserted_at: ~N[2018-05-05 11:39:49.688053], institute: #Ecto.Association.NotLoaded<association :institute is not loaded>, institute_id: 1, isbn: "13-1234-78", record_id: 17, records: #Ecto.Association.NotLoaded<association :records is not loaded>, status: :out, title: "Hello World", updated_at: ~N[2018-05-25 16:01:41.752519], year: 1982}
        (ecto) lib/ecto/multi.ex:421: Ecto.Multi.apply_operation/5
        (elixir) lib/enum.ex:1899: Enum."-reduce/3-lists^foldl/2-0-"/3
        (ecto) lib/ecto/multi.ex:411: anonymous fn/5 in Ecto.Multi.apply_operations/5
        (ecto) lib/ecto/adapters/sql.ex:576: anonymous fn/3 in Ecto.Adapters.SQL.do_transaction/3
        (db_connection) lib/db_connection.ex:1283: DBConnection.transaction_run/4
        (db_connection) lib/db_connection.ex:1207: DBConnection.run_begin/3
        (db_connection) lib/db_connection.ex:798: DBConnection.transaction/3
        (ecto) lib/ecto/repo/queryable.ex:23: Ecto.Repo.Queryable.transaction/4
        (mango) lib/mango/records/records.ex:112: anonymous fn/1 in Mango.Records.create_record_in/2 # referring to this line: |> Repo.transaction()
..
..

Any idea what might be going on here? Thank you.

Showing Posts 1 to 10

amnu3387

amnu3387

I think you just need to name your first Multi.run(:get_book... as Multi.run(:book... ?

acrolink

acrolink OP

I wil l try but I believe that is only a name, will not make any difference.

OvermindDL1

OvermindDL1

The name is how you reference that step in later steps though.

Ecto.Multi very much is a named monadic pipeline.

Elixir really needs some monad stuff built in better so umpteen libraries don’t have to keep remaking it and could just use a behaviour…

amnu3387

amnu3387

It could also be failing on your .get_zzz!s but if it’s that, when corrected it would still fail when you use fn %{book: book} as you need to use a key that refers to a previous multi “step” I guess - I just recently started using multi though

acrolink

acrolink OP

I have tried the suggestions above, like this:

  multi_result =
    Multi.new()
    |> Multi.run(:bookA, fn _ -> Mango.Books.get_book!(line.book_id) end)
    |> Multi.run(:bookB, fn %{bookA: bookA} ->
    IO.inspect bookA
      Books.update_book(Books.get_book!(bookA.id), %{status: 1})
    end)
    |> Multi.run(:update_associated_record, fn %{bookB: bookB} ->
      Records.update_record(Records.get_record!(bookB.record_id), %{
        returned_at: :os.system_time(:seconds)
      })
    end)
    |> Repo.transaction()
    |> case do
      {:ok, result} -> result
      {:ok, %{book: book}} -> book
      {:ok, %{record: record}} -> record |> whitelist_record_fields
      {:error, :book_to_be_returned, reason, _changes} -> reason
    end

But I am getting the same error message.

acrolink

acrolink OP

Solved..

I have to start with:

Multi.new()
|> Multi.update(:book, Book.changeset(Repo.get(Book, line.book_id), %{status: 1}))
OvermindDL1

OvermindDL1

A couple of things now that I have time to look:

  1. Books.update_book(Books.get_book!(book.book_id), %{status: 1})
    What is this returning? An {:ok, result} term?

  Records.update_record(Records.get_record!(book.record_id), %{
    returned_at: :os.system_time(:seconds)
  })

What is this returning? Also an {:ok, result} term?

      {:ok, %{book: book}} -> book
      {:ok, %{record: record}} -> record |> whitelist_record_fields

These will never ever be matched on because {:ok, result} -> result will grab it all first.

OvermindDL1

OvermindDL1

Update is a different kind of call, it is better to use though, but still, run requires {:ok, result}/{:error, reason} and I bet that one of those were not.

joaquinalcerro

joaquinalcerro

As @OvermindDL1 states, check the retuning value of the Multi.run functions:

Ecto.Multi — Ecto v3.14.0

OvermindDL1

OvermindDL1

Or rather what you return ‘from’ that function. :slight_smile:

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews