doodloo

doodloo

Ecto.Changeset.optimistic_lock and Ecto.Multi

I’m trying to make sense of something and the fact that I don’t understand it might prove that I’m using it wrong.

I’ve got various changeset functions exposed for an Ecto Schema. They add an optimistic lock check to the changeset structs.

As part of a large Ecto Multi, I noticed that when the update fails because of a lock mismatch, an exception is raised rather than an error tuple.
This kind of defeats the purpose of using an Ecto Multi in the first place, as the goal is to have the result of multi |> Repo.transaction() piped into a case statement, to gracefully let the user know what’s happening.

What is wrong in my logic?

Marked As Solved

LostKobrakai

LostKobrakai

So the issue with optimistic locks is that you don’t get to know that the lock field was the actual issue causing the more general error of trying to update a stale record. E.g. the original record could’ve been deleted in the meantime as well. So the changeset functionality around optimistic_lock cannot handle the resulting error. There is a stale_error_field config on Repo.update though, which you can use to catch the exception and instead have it reported as an error.

Last Post!

doodloo

doodloo

Thanks a lot @LostKobrakai . Sorry for the late answer!

Where Next?

Popular in Questions Top

New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement