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 , 9 years ago
Cc: | added |
---|---|
Component: | Uncategorized → Database layer (models, ORM) |
Type: | Uncategorized → Bug |
comment:2 by , 9 years ago
comment:3 by , 9 years ago
Cc: | added |
---|
comment:4 by , 9 years ago
Cc: | added |
---|
comment:5 by , 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 , 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 , 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 , 9 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Per a comment by akaariai on #23076:
This might be related to #25505 and #18012.