Opened 5 years ago

Closed 5 years ago

#25520 closed Bug (duplicate)

Cascade delete of proxy model children fails with IntegrityErrors

Reported by: James Murty Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: james@…, tzanke@…, jernej@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When deleting a concrete model based on proxy models, Django's deletion collector does not find M2M reverse relationships to proxy parents; it only finds these relationships for concrete parent models.

This means that deleting a proxy-based model object with extant M2M relationships targeting a proxy parent will fail with integrity errors if you use a database like PostgreSQL that enforces foreign key constraints. The target model instance cannot be deleted because the M2M FK relationships are not also deleted at the same time, and would otherwise remain pointing to a non-existent record.

This issue tends to affect users of django-polymorphic who rely more than most on proxy models, and has been reported in this context in #23076

I have created a Pull Requests based on the current master branch that adds unit tests demonstrating the issue when run against a database that enforces foreign key constrains, such as PostgreSQL: ​https://github.com/django/django/pull/5404

This issue also affects versions 1.7 and 1.8 and perhaps also earlier versions, but likely has not been encountered broadly because the use case that triggers it is somewhat unusual.

Change History (8)

comment:1 Changed 5 years ago by James Murty

Cc: james@… added
Component: UncategorizedDatabase layer (models, ORM)
Type: UncategorizedBug

comment:2 Changed 5 years ago by James Murty

Per a comment by akaariai on #23076:

This might be related to #25505 and #18012.

comment:3 Changed 5 years ago by TZanke

Cc: tzanke@… added

comment:4 Changed 5 years ago by Jernej Kos

Cc: jernej@… added

comment:5 Changed 5 years ago by James Murty

The linked pull request now includes a patch to fix this issue for deletion: https://github.com/ixc/django/commit/47aa8c6ec276965eccfe3a52694109d8258562de

comment:6 Changed 5 years ago by Simon Charette

This is really just a symptom of the issue described in #23076 and #25505 and should be closed as a duplicate.

IMO the correct fix would be to propagate reverse foreign keys from proxy models to their concrete base (#18012) and then simply make sure the proxy model deletion code always retrieve the reverse relationships from concrete models.

comment:7 Changed 5 years ago by James Murty

FWIW the PR proposed fix for #18012 looks reasonable to me: propagating the reverse FKs of proxy models to their base seems to match assumptions made in the code for deletion. However I am not familiar enough with the code and issues to properly understand any implications or trade-offs, so I won't weigh in over there.

comment:8 Changed 5 years ago by Tim Graham

Resolution: duplicate
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top