简体   繁体   English

在密集数据库中撤消级联删除

[英]Undoing cascade deletions in a dense database

I have a fairly large production database system, based on a large hierarchy of nodes each with a 10+ associated models. 我有一个相当大的生产数据库系统,基于大型节点层次结构,每个节点都有10个以上的关联模型。 If someone deletes a node fairly high in the tree, there can be thousands of models deleted and if that deletion was a mistake, restoring them can be very difficult. 如果有人删除树中相当高的节点,则可能会删除数千个模型,如果删除错误,则恢复它们可能非常困难。 I'm looking for a way to give me an easy 'undo' option. 我正在寻找一种方法来给我一个简单的“撤消”选项。

I've tried using Django-reversion, but it seems like in order to get the functionality I want (easily reverting a large cascade delete) it needs to store a bunch of information with each revision. 我尝试过使用Django-reversion,但似乎为了获得我想要的功能(很容易恢复大型级联删除),它需要在每个版本中存储一堆信息。 When I created initial revisions, the process is less than 10% done and it's already using 8GB in my database, which is not going to work for me. 当我创建初始修订时,该过程不到10%完成,并且它已经在我的数据库中使用8GB,这对我不起作用。

So, is there a standard solution for this problem? 那么,这个问题有一个标准的解决方案吗? Or a way to customize Django-reversions to fit my use case? 还是一种自定义Django-reversions以适应我的用例的方法?

What you're looking for is called a soft delete. 您正在寻找的是一种软删除。 Add a column named deleted with a value of false to the table. 向表中添加名为deleted的列,其值为false。 Now when you want to do a "delete" instead change the column deleted to true. 现在,当您想要执行“删除”而不是将deleted的列更改为true时。 Update all the code not to show the rows marked as deleted (or move the database table and replace it with a view that doesn't show them). 更新所有代码不显示标记为已删除的行(或移动数据库表并将其替换为不显示它们的视图)。 Change all the unique constraints to have a filter WHERE deleted = false so you won't have a problem with not being able to add something similar to what user can't see in the system. 更改所有唯一约束以使过滤器WHERE deleted = false这样您就不会遇到无法添加类似于用户在系统中看不到的内容的问题。

As for the cascades you have two options. 至于级联,你有两个选择。 Either do an ON UPDATE trigger that will update the child rows or add the deleted column to the FK and define it as ON UPDATE CASCADE . 执行ON UPDATE触发器以更新子行或将deleted列添加到FK并将其定义为ON UPDATE CASCADE

You'll get the whole reverse functionality at a cost of one extra row (and not being able to delete stuff to save space unless you do it manually). 您将以一个额外的行为代价获得整个反向功能(除非您手动执行此操作,否则无法删除内容以节省空间)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM