[Answered ]-(Django) Does MySQL foreign key cascade delete parent or children first

2👍

What Django does when you delete an object -> It deletes all children first, and then deletes the object.

Your schema is a little bit strange, you need to make your last_child object null-able, because how are you gonna create a parent without children ?

You can’t create a child without a parent, and you can’t create a parent with a last_child.

So when you make your last_child null-able, on parent.delete you need to set last_child = null, and then call parent.delete(). And I guess you will be fine.

👤Todor

Leave a comment