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

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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

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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

We're in Beta

About us Mission Statement