﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
27585	ManyToMany relationship is cascading delete after being detached	Teresa	nobody	"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'.

"	Bug	closed	Database layer (models, ORM)	1.8	Normal	invalid	ManyToMany, cascade, delete, remove		Unreviewed	0	0	0	0	0	0
