[Django] #31285: Inherited model doesn't correctly order by "-pk" when specified on Parent.Meta.ordering

30 views
Skip to first unread message

Django

unread,
Feb 18, 2020, 10:00:05 PM2/18/20
to django-...@googlegroups.com
#31285: Inherited model doesn't correctly order by "-pk" when specified on
Parent.Meta.ordering
-------------------------------------+-------------------------------------
Reporter: Jon | Owner: nobody
Dufresne |
Type: Bug | Status: new
Component: Database | Version: master
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Given the following model definition:

{{{
from django.db import models

class Parent(models.Model):
class Meta:
ordering = ["-pk"]

class Child(Parent):
pass
}}}

Querying the `Child` class results in the following:

{{{
>>> print(Child.objects.all().query)
SELECT "myapp_parent"."id", "myapp_child"."parent_ptr_id" FROM
"myapp_child" INNER JOIN "myapp_parent" ON ("myapp_child"."parent_ptr_id"
= "myapp_parent"."id") ORDER BY "myapp_parent"."id" ASC
}}}

The query is ordered `ASC` but I expect the order to be `DESC`.

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

Django

unread,
Feb 18, 2020, 11:24:43 PM2/18/20
to django-...@googlegroups.com
#31285: Inherited model doesn't correctly order by "-pk" when specified on
Parent.Meta.ordering
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* stage: Unreviewed => Accepted


Comment:

Interesting `ordering` is effectively one of the rare `Meta`
[https://docs.djangoproject.com/en/3.0/topics/db/models/#meta-and-multi-
table-inheritance attribute preserved during MTI inheritance].

I also wonder if `ordering = ('pk',)` should be special cased to order by
the ''local'' child class primary key which would be
`"myapp_child"."parent_ptr_id"` in this case since it should result in the
same resulset because of foreign key integrity and avoid an unnecessary
`JOIN` in the cases where no parent fields are involved.

--
Ticket URL: <https://code.djangoproject.com/ticket/31285#comment:1>

Django

unread,
Feb 19, 2020, 12:08:55 AM2/19/20
to django-...@googlegroups.com
#31285: Inherited model doesn't correctly order by "-pk" when specified on
Parent.Meta.ordering
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jon Dufresne):

* has_patch: 0 => 1


Comment:

https://github.com/django/django/pull/12470

> I also wonder if `ordering = ('pk',)` should be special cased to order
by the ''local'' child class primary key which would be
`"myapp_child"."parent_ptr_id"` in this case since it should result in the
same resulset because of foreign key integrity and avoid an unnecessary
`JOIN` in the cases where no parent fields are involved.

Interesting enough, ordering by `["-id"]` was already doing that. So I
agree that "-pk" should behave the same.

--
Ticket URL: <https://code.djangoproject.com/ticket/31285#comment:2>

Django

unread,
Feb 19, 2020, 2:22:03 PM2/19/20
to django-...@googlegroups.com
#31285: Inherited model doesn't correctly order by "-pk" when specified on
Parent.Meta.ordering
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

I would expect `Parent.meta.ordering = ["id"]` to result in `ORDER BY
"myapp_parent"."id"` while `Parent.meta.ordering = ["pk"]` results in
`ORDER BY "myapp_child"."parent_ptr_id"`.

Isn't that the case right now?

--
Ticket URL: <https://code.djangoproject.com/ticket/31285#comment:3>

Django

unread,
Feb 20, 2020, 10:37:36 PM2/20/20
to django-...@googlegroups.com
#31285: Inherited model doesn't correctly order by "-pk" when specified on
Parent.Meta.ordering
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Jon
| Dufresne
Type: Bug | Status: assigned

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Abhijeet):

* owner: nobody => Jon Dufresne
* status: new => assigned


Comment:

I'm changing the owner and status since there already is a patch by the
reporter.

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

Django

unread,
Mar 19, 2020, 8:17:21 AM3/19/20
to django-...@googlegroups.com
#31285: Inherited model doesn't correctly order by "-pk" when specified on
Parent.Meta.ordering
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Jon
| Dufresne
Type: Bug | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"013147fae2b9168b06c495aa78c10725cab294cd" 013147fa]:
{{{
#!CommitTicketReference repository=""
revision="013147fae2b9168b06c495aa78c10725cab294cd"
Fixed #31285 -- Fixed inherited Meta.ordering of "-pk".
}}}

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

Reply all
Reply to author
Forward
0 new messages