Understanding Ecto's on_replace and Using Deferred Constraints

87
clicks
Understanding Ecto's on_replace and Using Deferred Constraints

Source: katafrakt.me

Type: Post

The article recounts the author's experience with Ecto, focusing on a scenario where processes have ordered steps that need to be managed accurately. The initial implementation involved creating Ecto schemas for processes and steps, with an attempt to enforce unique ordering through database constraints. However, when trying to reorder steps, the author encountered unique index violations because of the way Ecto handles updates. The author explored options to resolve this issue and discovered the concept of deferrable constraints in PostgreSQL. By replacing a unique index with a deferrable uniqueness check, they were able to allow Ecto to correctly update the step order without triggering constraint violations immediately. This led to a successful solution while highlighting the importance of understanding database constraints and how they interact with application logic.

© HashMerge 2025