Last Updated: February 25, 2016
·
3.452K
· felipeelias

Soft delete in postgresql using rules

If you want to mark a row of your database as being "deleted" instead of actually removing it, you can create a rule that does that:

CREATE OR REPLACE RULE delete_venue AS
  ON DELETE TO venues DO INSTEAD
    UPDATE venues
    SET active = 'f'
    WHERE venues.venue_id = old.venue_id;

This way the active column will be set to false instead of removing the row from the database.

Another way would use a field named deleted_at and set it to the current date, so you would know when it was deleted.


endorse