Opened 2 years ago

Closed 2 years ago

#33271 closed Bug (duplicate)

Bulk delete does not delete related objects

Reported by: Lukas Klement Owned by: nobody
Component: Database layer (models, ORM) Version: 3.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

Change History (1)

comment:1 by Mariusz Felisiak, 2 years ago

Component: UncategorizedDatabase layer (models, ORM)
Resolution: duplicate
Status: newclosed
Type: UncategorizedBug

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.

Note: See TracTickets for help on using tickets.
Back to Top