postgresqlerlangelixirphoenix-frameworkecto

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.


Solution

  • 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.