I’ve got a problem with something I written to keep track of stuff, a couple months ago.
You can add messages to tasks, and the tasks get completed, the messages all remain, that’s fine, so tasks that are incomplete, or complete have their messages associated by the task_id in the messages table. The problem is, if someone deletes a task, it doesn’t delete the messages that were attached to it by it’s ID.
So I’ve now got lots of messages that don’t have an “owner”, are just lying redundant in the database so I need to somehow delete all from messages where messages.task_id doesn’t exist in job.id. Hope my pseudo code explains what I’m in need of doing.
Thanks.
First, back up the Messages table.
Then try this to see if the messages selected make sense to delete
If they do, then Delete
If you can enforce Refential Integrity between Tasks and Messages using a Foreign Key, you will avoid the “Orphaned Records” issue in the future.