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

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
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
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
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

We're in Beta

About us Mission Statement