1

In the Changeset documentation, it says:

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.

There are a number of conversations on StackOverflow, GitHub, and other places about how to fix various scenarios that involve a StaleEntryError. But there is no explanation of how Ecto knows that a struct is stale.

What is the underlying mechanism?

1 Answer 1

2

The Ecto.Changeset documentation you linked to is pretty self explanatory:

Optimistic locking works by keeping a “version” counter for each record; this counter gets incremented each time a modification is made to a record. Hence, in order to use optimistic locking, a field must exist in your schema for versioning purpose. Such field is usually an integer but other types are supported.

Here's the Ecto implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.