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