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?

Where Next?

Popular in Questions Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
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
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
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
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement