Home > OS >  Django - What happens when you delete an entity in a table that has a foreign key ManyToMany relatio
Django - What happens when you delete an entity in a table that has a foreign key ManyToMany relatio

Time:12-03

I've noticed that Django doesn't let you set the on_delete parameter for ManyToManyField. This made me curious what happens if you delete an entity in a ManyToMany relationship? So for example, let's say we have Book, which has a ManyToMany relationship to Author. Let's say that a book A has 3 authors: "Tom Lam", "Lam Tom" and "Britney Britney". Then let's say "Britney Britney" gets deleted from the Author table. Does "Britney Britney" get removed from the ManyToMany relationship with Book? Does an exception get thrown when trying to delete "Britney Britney?" What does Django do in the case that an entity is deleted when it exists in a ManyToMany relationship?

CodePudding user response:

If "Britney Britney" is deleted from the Author table, "Britney Britney" is also removed from the book authors (this also applies in reverse). Django does not throw an exception, simply the deleted instance will no longer be accessible through the many-to-many relationship.

  • Related