acrolink

acrolink

Strange error in Ecto.Multi

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.

Marked As Solved

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.

Also Liked

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.

amnu3387

amnu3387

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

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…

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

Other popular topics Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement