简体   繁体   中英

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. 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.

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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