﻿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
33271	Bulk delete does not delete related objects	Lukas Klement	nobody	"Calling Model.objects.delete() causes integrity errors with ForeignKey and ManyToMany related objects, despite specifying on_delete=models.CASCADE for the ForeignKey.

Here's the models:

{{{
FirstModel(models.Model):
    many = models.ManyToManyField('app.SecondModel', related_name='order_diets', blank=True)

ThirdModel(models.Model):
    first = models.ForeignKey(FirstModel, on_delete=models.CASCADE, related_name='related)

SecondModel(models.Model):
    pass
}}}


Calling the bulk delete:
{{{
existing = FirstModel.objects.filter(...)
existing.delete()
}}}

... causes the following error:

{{{
IntegrityError at /mypath/.../action/
update or delete on table ""..."" violates foreign key constraint ""app_first_model_many_many_id_3a17178e_fk_app_first_model_id"" on table ""app_first_model_many""
DETAIL:  Key (id)=(123456789) is still referenced from table ""app_first_model_many"".
}}}


This issue seems to have been reported previously: https://code.djangoproject.com/ticket/31600

Django version: 3.2.8"	Bug	closed	Database layer (models, ORM)	3.2	Normal	duplicate			Unreviewed	0	0	0	0	0	0
