#34796 closed Bug (duplicate)
Deleting child table does not delete rows in parent table when using multi-table inheritance
Reported by: | Stephen Finucane | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 4.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 (last modified by )
If you have a child model that depends on a parent model via multi-table inheritance, then deleting the child will not delete rows in the parent model's table.
I am aware that we don't use database-level cascades (ticket:21961) but I expected this to be implemented by Django in Python, given the docs (https://docs.djangoproject.com/en/4.2/topics/db/models/#multi-table-inheritance) say that the automatically-created OneToOneField
on the child model will have on_delete=models.CASCADE
.
place_ptr = models.OneToOneField( Place, on_delete=models.CASCADE, parent_link=True, primary_key=True, )
I have a minimal reproducer which can be found at https://github.com/stephenfin/django-bug-34796. In summary though, you simply need to create a table using multi-table inheritance and then delete that table. While the child table will be deleted, the corresponding rows in the parent table will remain.
Change History (7)
comment:1 by , 16 months ago
Description: | modified (diff) |
---|
comment:2 by , 16 months ago
Summary: | Deleting child table does not delete columns in parent table when using multi-table inheritance → Deleting child table does not delete rows in parent table when using multi-table inheritance |
---|
comment:3 by , 16 months ago
Description: | modified (diff) |
---|
comment:4 by , 16 months ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
follow-up: 6 comment:5 by , 16 months ago
Thanks, Mariusz. However, there is no reference to this anywhere in the documentation, so at a minimum it is very surprising behaviour that is likely to catch users out. Until we fix this in a backwards-incompatible manner, would you be open to a documentation patch that calls this out? Alternatively (better), could we log a warning when auto-generating migrations that remove child tables in a multi-table relationship to indicate this potential issue and suggest users clean it up accordingly?
comment:6 by , 16 months ago
Replying to Stephen Finucane:
Thanks, Mariusz. However, there is no reference to this anywhere in the documentation, so at a minimum it is very surprising behaviour that is likely to catch users out.
I don't see anything unexpected in the current behavior. TBH, I would be surprised if migrations manipulated the data in tables on its own.
Thanks for the ticket, however migrations doesn't automatically manipulate (delete/update/add) data. Generated migrations are responsible for propagating changes in the database structure (DDL). Any changes to this would be backward incompatible. #21961 is the ticket that must be implemented (as you already noticed) if you want to tell your db that related data should be deleted in cascade.
Marking as a duplicate because #21961 will effectively fix this, for users who opt-in for the cascade behavior.