patrickdm

patrickdm

Hello,
I’ve been trying to implement Ecto.Changeset.optimistic_lock in a Phoenix application but I’m stuck with the generated controller’s update action.

In iex I have success rising an Ecto.StaleEntryError when I try to update a stale record, (as shown in the documentation):

iex> post = Repo.insert!(%Post{title: "foo"})
%Post{id: 1, title: "foo", lock_version: 1}
iex> valid_change = Post.changeset(:update, post, %{title: "bar"})
iex> stale_change = Post.changeset(:update, post, %{title: "baz"})
iex> Repo.update!(valid_change)
%Post{id: 1, title: "bar", lock_version: 2}
iex> Repo.update!(stale_change)
** (Ecto.StaleEntryError) attempted to update a stale entry

here the two changeset are being generated from the same post struct, and Repo.update raise the expected error.

On the other hand, when the update is performed concurrently in two browser windows, -my understanding is that- the controller’s update action performs a new get_post!(id) and then uses that struct to make the update’s changeset, with the form params; therefore the latest update is always overwriting the first occurring, thus making optimistic_lock useless in this context.

I couldn’t find much help on this issue, and it make me think I’m getting all this wrong.. Thank’s for any help.

Showing Posts 1 to 4

1player

1player

That’s an interesting question. Disclaimer: I’ve never used optimistic_lock.

Since lock_version is just a regular field, one idea would be to pass, alongside with the ID, the version of the object in the update method and using that version before you apply any changes.

Example, in your form:

<form action="<%= Routes.post_path(@conn, :update, @post) %>" method="POST">
  ...
  <input type="hidden" name="post[lock_version]" value="<%= @post.lock_version %>"> 
</form>

And in your controller:

def update(conn, %{"id" => id, "post" => %{"lock_version" => lock_version}}) do
  post = 
    id
    |> Posts.get_post!()
    |> Map.put(:lock_version, lock_version)

  ...
end

Now you will will get a StaleEntryError if you’re updating an old post.

EDIT: you might want to use hidden_input to generate the hidden field instead of doing it manually

patrickdm

patrickdm OP

Hello 1player and thank you,

Since lock_version is just a regular field, one idea would be to pass, alongside with the ID, the version of the object in the update method and using that version before you apply any changes [..]

post = 
  id
  |> Posts.get_post!()
  |> Map.put(:lock_version, lock_version)

Oh :astonished:, right, that easy :man_facepalming:

Replacing the validly-updated new :lock_version with the stale one after get_post!(id)… I’m going to try this now, sure it is going to work!

I’ve been stubbornly poking around with attempts to pass the post struct assigned in edit, from the form to the update action, with no success, and I couldn’t think simple again. Thank you for your help!

Kurisu

Kurisu

Hello @patrickdm! I was wondering if you could share your approach to managing the Ecto.StaleEntryError exception within a controller or a live view?

I came across a similar issue discussed in another thread during my research, but unfortunately, it didn’t have any responses. Your insights would be greatly appreciated.

Kurisu

Kurisu

After conducting further research, I believe I’ve found a suitable solution to my question, particularly in relation to LiveView.

Here’s the section I found in the docs: Error and exception handling — Phoenix LiveView v1.2.5

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews