Electronics & Programming


Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » ON DELETE CASCADE behaviour on M2M? [RSS Feed]

#1 Jan. 18, 2011 06:36:41

Victor H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

ON DELETE CASCADE behaviour on M2M?


I know that Django's default behaviour on ForeignKey is ON DELETE CASCADE
(unless you set on_delete to PROTECT). However, I wasn't sure how this
extended to ManyToManyFields.

I've just tested with one of my own applications - I have an "Article"
object, with m2m to a "Journalist" object. Deleting either end of the m2m
didn't affect the other end.

I assume this is intended behaviour - since it simply cascades to the
invisible join table in the middle, but through to the other actual end of
the relatinoship?

However, the issue I see with this is that if the m2m can't be empty (as is
default). You delete one end, and everything seems fine and dandy. But then
you go into edit the other end of the relationship, and it now complains
that the m2m field is empty. But during that period until you tried to
open/edit it, it was fine with having a empty mandatory m2m field. Surely
there's an integrity issue there?

Secondly, is there a way to tweak the ON DELETE behaviour for m2m. E.g. can
you set it to PROTECT, just like you do for FK's, to prevent you deleting an
object, if something else has a m2m link to it? Or can you set it to ON
CASCADE DELETE, as it does for FK?


You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.


Board footer

Moderator control

Enjoy the 13th of December

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support