Soft delete best practices (PHP/MySQL)

前端 未结 6 2094
予麋鹿
予麋鹿 2020-12-02 18:32

Problem

In a web application dealing with products and orders, I want to maintain information and relationships between former employees (users) and

6条回答
  •  一生所求
    2020-12-02 19:18

    Your idea does make sense and is used frequently in production but, to implement it you will need to update quite a bit of code to account for the new field. Another option could be to archive (move) the "soft-deleted" records to a separate table or database. This is done frequently as well and makes the issue one of maintenance rather than (re)programming. (You could have a table trigger react to the delete to archive the deleted record.)

    I would do the archiving to avoid a major update to production code. But if you want to use deleted-flag field, use it as a timestamp to give you additional useful info beyond a boolean. (Null = not deleted.) You might also want to add a DeletedBy field to track the user responsible for deleting the record. Using two fields gives you a lot of info tells you who deleted what and when. (The two extra field solution is also something that can be done in an archive table/database.)

提交回复
热议问题