Opened 9 years ago

Closed 9 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: dev
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 by James Murty, 9 years ago

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

comment:2 by James Murty, 9 years ago

Per a comment by akaariai on #23076:

This might be related to #25505 and #18012.

comment:3 by TZanke, 9 years ago

Cc: tzanke@… added

comment:4 by Jernej Kos, 9 years ago

Cc: jernej@… added

comment:5 by James Murty, 9 years ago

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

comment:6 by Simon Charette, 9 years ago

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 by James Murty, 9 years ago

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 by Tim Graham, 9 years ago

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