[Django] #33271: Bulk delete does not delete related objects

24 views
Skip to first unread message

Django

unread,
Nov 8, 2021, 12:54:52 PM11/8/21
to django-...@googlegroups.com
#33271: Bulk delete does not delete related objects
-----------------------------------------+------------------------
Reporter: Lukas Klement | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
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

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

Django

unread,
Nov 8, 2021, 1:29:07 PM11/8/21
to django-...@googlegroups.com
#33271: Bulk delete does not delete related objects
-------------------------------------+-------------------------------------

Reporter: Lukas Klement | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: duplicate

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => duplicate
* component: Uncategorized => Database layer (models, ORM)
* type: Uncategorized => Bug


Comment:

Thanks for the report, however I cannot reproduce this with provided
models. Please reopen the original ticket #31600 if you can debug your
issue and provide a sample project to reproduce it.

Duplicate of #31600.

--
Ticket URL: <https://code.djangoproject.com/ticket/33271#comment:1>

Reply all
Reply to author
Forward
0 new messages