Home > Enterprise >  Soft Delete. Move records or create "deleted" column
Soft Delete. Move records or create "deleted" column

Time:10-05

I am a little confused about which is better for soft delete. There are two ways for Soft Delete.

  1. create table for deleted records.(In this way we will make copy for the records in the table of deleted records, then delete it from its table)
  2. create extra column called deleted,(In this way we will only change the status of this field to true , then at display records we will filter according to this extra field)

Also, I want to store the changes of the records after every update, So I think creating extra table is more suitable. What is your opinion?

CodePudding user response:

Best option is the second one, in your first example it's not a soft delete if your deleting it from the table - soft should be to modify the data in a minimal way. Leaving the row in place is the purpose of a soft-delete, this has the minimal effect on the data and will retain all attributes such as primary key index value and any internals you cant see that the database might use.

Your first option is far less succinct as it means duplicating data structures. A common approach is to add a "deleted_at" column (default to NULL), this positively identifies the record state.

CodePudding user response:

I agree with @web-engineer, adding a nullable column with the datetime of when the row has been soft-deleted is the best. I used this ressource to do this.

And to answer the second part of your question, yes an extra table will be needed. There is a third party app named django-simple-history which handles it for you.

  • Related