Ecto.StaleEntryError when updating
Asked Answered
A

2

11

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.

Amelioration answered 16/4, 2017 at 16:23 Comment(0)
S
17

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.
Scheer answered 16/4, 2017 at 18:27 Comment(1)
That was my mistake, I passed record from one process to another without "reloading" it. My bad.Amelioration
F
0

Other possibility may occurr if you manipulate multiple Repos and you're bulding tests. In my case, i was writing some tests, and i've accidently selected differents Repos, so it was giving this error.

Furnivall answered 13/2 at 13:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.