Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ecto.StaleEntryError when updating

Sometimes I get (Ecto.StaleEntryError) attempted to update a stale struct when trying to update record. I haven't found any recipe to avoid this in Ecto's docs, so what should I do to avoid this?

P.S. I'm using PostgreSQL 9.6 via Postgrex, Ecto 2.1.3.

like image 810
Egor Biriukov Avatar asked Apr 16 '17 16:04

Egor Biriukov


1 Answers

Looks like you are fetching a schema from the database and updating it after it has been updated somewhere else in your app. Are you fetching, hanging onto it, and updating later? If so, fetch, change, and update.

Right from the Ecto docs:

When a conflict happens (a record which has been previously fetched is
being updated, but that same record has been modified since it was
fetched), an `Ecto.StaleEntryError` exception is raised.
like image 138
Steve Pallen Avatar answered Sep 28 '22 11:09

Steve Pallen