Implementing Soft Deletes in Ecto with PostgreSQL Rules and Views

279
clicks
Implementing Soft Deletes in Ecto with PostgreSQL Rules and Views

Source: dashbit.co

Type: Post

José Valim explains a method to implement soft deletes in an Elixir application using Ecto with PostgreSQL. Soft deletes involve marking a record as deleted without physically removing it from the database. This article introduces adding a `deleted_at` column to the database schema and using PostgreSQL rules to transform delete operations into updates setting the `deleted_at` timestamp. To ensure queries do not return soft-deleted records, Valim suggests creating PostgreSQL views that filter out these records. The article also covers handling Ecto's behavior towards soft-deleted records, particularly the potential issue with `Ecto.StaleEntryError`. Lastly, it provides techniques for permanently deleting records by disabling the soft delete rule momentarily.

© HashMerge 2024