1stSolo

1stSolo

Optimistic Locking clarification?

Does lock_version need to be cast() into ChangeSet, or doing

changeset
|> optimistic_lock(:lock_version)

is enough?

I mean, AFAIK every attempt to update an object should have lock_version in the attributes map, so how does Ecto know what value to use for lock_version if we don’t put it into changeset by casting it?..

But when I look at examples online and in documentation all I see is ChangeSet.optimistic_lock/3 being called.

Most Liked

1stSolo

1stSolo

In my case I have a absinthe client that pulls the object from the backend via graphql, and edits some fields on the object. The save operation takes id of the object, the changed field, e.g. description, and lockVersion from the last time the data was pulled. Multiple users can edit the same object, hence optimistic locking to prevent overwriting someone else’s changes.
So lockVersion actually comes as part of parameters/arguments along with new value for description.
Therefore I thought the right way to do it is to

%Object{id: params.id}
|> Ecto.Changeset.cast(params.data, [:description, :lock_version])
|> Ecto.Changeset.optimistic_lock(:lock_version)

which should result in Ecto preparing a SQL statement such as
UPDATE Object SET description=?, lock_version=? WHERE id=? AND lock_version=?, and binding params.data.description into 1st arg, incremented lock_version into second arg, params.id into 3rd arg and lockVersion from the client side into the last arg. Then if updated record count returned by the database driver is 1 then update succeeded and if it’s 0 then Ecto would raise StaleStateError.

So I actually do want to set lockVersion from the params. Is that incorrect?

Last Post!

woylie

woylie

Yes, in your case, you’d want to cast the lock version indeed.

Where Next?

Popular in Questions Top

Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement