简体   繁体   English

MySQL外键,跳出“级联删除”而不删除原始记录

[英]MySQL foreign key, trip “Cascade Delete” without removing original record

I am currently working in a system where we are using MySQL to store all of our relational data, but our audit logs are being kept in a MongoDB document database for long term archival. 我目前正在使用MySQL来存储所有关系数据的系统中工作,但是我们的审核日志仍保存在MongoDB文档数据库中以进行长期存档。 In the case of 'projects' (the largest parent entity in the system) I want to allow users to delete project and force the cascade delete to fire effectively cleaning my database of all related data. 对于“项目”(系统中最大的父实体),我想允许用户删除项目并强制级联删除以有效清除所有相关数据的数据库。 (This is where it gets tricky) (这是棘手的地方)

Ideally, once deleted, the singular record in my 'Projects' table would persist through the delete, get flagged as an 'archived' project and my code would then know that if a user tries to access an archived project we would load a single page showing all of the MongoDB audit logs that were collected during the life of the project prior to deletion. 理想情况下,一旦删除,“项目”表中的单个记录将通过删除保留下来,被标记为“已归档”项目,然后我的代码将知道,如果用户尝试访问已归档的项目,我们将加载单个页面显示在项目生命周期中删除之前收集的所有MongoDB审核日志。

I think I could make this work in a transaction by selecting the project record, running the delete, re-inserting the data and re-using the original AutoInc id that was assigned to the project. 我认为我可以通过选择项目记录,运行删除,重新插入数据并重新使用分配给项目的原始AutoInc ID来使这项工作有效。 Something about this FEELS dirty, so I want to know if anyone sees a better way to do it. 关于这种感觉很脏,所以我想知道是否有人认为有更好的方法可以做到这一点。

Ideally if there were a way to trigger the cascade event on a table's foreign key without actually removing the original record I would have what I need, but I haven't seen any such beast. 理想情况下,如果有一种方法可以在不真正删除原始记录的情况下触发表的外键上的层叠事件,那么我将拥有所需的东西,但是我还没有看到任何此类野兽。

Thanks for your help! 谢谢你的帮助!

What I've used in the past is just add an archive column and make it a boolean. 我过去使用的只是添加一个存档列并将其设置为布尔值。 So when the user 'deletes' something, it only toggles the flag. 因此,当用户“删除”某物时,它只会切换该标志。 Obviously this can only be done if you're using something like php to handle the request and not allowing users to directly modify the database. 显然,只有在使用php之类的东西来处理请求并且不允许用户直接修改数据库的情况下,才能做到这一点。 Need some more info on how you plan on doing this. 需要更多有关如何计划执行此操作的信息。

I would put this in the comments but I don't think I have a high enough rating to comment on other questions. 我会将其放在评论中,但我认为我没有足够高的评分来评论其他问题。

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

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