Re: [Django] #34796: Deleting child table does not delete rows in parent table when using multi-table inheritance

21 views
Skip to first unread message

Django

unread,
Aug 24, 2023, 10:59:36 AM8/24/23
to django-...@googlegroups.com
#34796: Deleting child table does not delete rows in parent table when using multi-
table inheritance
-------------------------------------+-------------------------------------
Reporter: Stephen Finucane | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Stephen Finucane:

Old description:

> 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.

New description:

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.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/34796#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 25, 2023, 6:48:53 AM8/25/23
to django-...@googlegroups.com
#34796: Deleting child table does not delete rows in parent table when using multi-
table inheritance
-------------------------------------+-------------------------------------
Reporter: Stephen Finucane | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: duplicate

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => duplicate


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/34796#comment:4>

Django

unread,
Aug 25, 2023, 7:39:21 AM8/25/23
to django-...@googlegroups.com
#34796: Deleting child table does not delete rows in parent table when using multi-
table inheritance
-------------------------------------+-------------------------------------
Reporter: Stephen Finucane | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by 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. 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?

--
Ticket URL: <https://code.djangoproject.com/ticket/34796#comment:5>

Django

unread,
Aug 25, 2023, 3:22:12 PM8/25/23
to django-...@googlegroups.com
#34796: Deleting child table does not delete rows in parent table when using multi-
table inheritance
-------------------------------------+-------------------------------------
Reporter: Stephen Finucane | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

Replying to [comment:5 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.

--
Ticket URL: <https://code.djangoproject.com/ticket/34796#comment:6>

Django

unread,
Aug 25, 2023, 7:43:43 PM8/25/23
to django-...@googlegroups.com
#34796: Deleting child table does not delete rows in parent table when using multi-
table inheritance
-------------------------------------+-------------------------------------
Reporter: Stephen Finucane | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

Also a dupe of #32724.

--
Ticket URL: <https://code.djangoproject.com/ticket/34796#comment:7>

Reply all
Reply to author
Forward
0 new messages