#27585: ManyToMany relationship is cascading delete after being detached
-------------------------------------+-------------------------------------
               Reporter:  Teresa     |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  1.8
  layer (models, ORM)                |       Keywords:  ManyToMany,
               Severity:  Normal     |  cascade, delete, remove
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Given two models A and B that have a many-to-many relationship, removing
 all of the As from a model B, then deleting B results in all of the As
 that should now be detached from B being deleted. This is happening inside
 of a single transaction, using an SQLite database backend, as well as a
 MySQL backend.

 Below is code that will reproduce the issue:

 {{{
 class A(models.Model):
   bs = models.ManyToManyField(B, related_name='as')

 class B(models.Model):
   ...

 def delete_b_without_deleting_as(b):
   with transaction.atomic():
     to_be_orphaned_as = [a for a in b.as.all()]
     for a in to_be_orphaned_as:
       b.as.remove(a)
     b.delete()
 }}}

 This results in both 'b' and all of the 'a's that had been a part of
 b.as.all() to be deleted from the database.

 The expected behaviour would be for b.delete() to not cascade as it is a
 ManyToMany field, and in addition all of the 'a's have been detached from
 the relationship with 'b'. There is no reason for deleting 'b' to cascade
 and delete models that no longer have any relationship with 'b'.

--
Ticket URL: <https://code.djangoproject.com/ticket/27585>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.bbc91dffcb4776031f5a161e1a3a511a%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to