> It is also possible to order a queryset by a related field, without
incurring the cost of a JOIN, by referring to the _id of the related
field:
> {{{
> # No Join
> Entry.objects.order_by('blog_id')
>
> # Join
> Entry.objects.order_by('blog__id')
> }}}
However, in recent versions the latter query is optimised and won't create
a join:
{{{
>>> print(Bar.objects.order_by('foo__id').query)
SELECT "foo_bar"."id", "foo_bar"."foo_id" FROM "foo_bar" ORDER BY
"foo_bar"."foo_id" ASC
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28561>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Old description:
> The documentation on `order_by()` states:
>
> > It is also possible to order a queryset by a related field, without
> incurring the cost of a JOIN, by referring to the _id of the related
> field:
> > {{{
> > # No Join
> > Entry.objects.order_by('blog_id')
> >
> > # Join
> > Entry.objects.order_by('blog__id')
> > }}}
>
> However, in recent versions the latter query is optimised and won't
> create a join:
>
> {{{
> >>> print(Bar.objects.order_by('foo__id').query)
> SELECT "foo_bar"."id", "foo_bar"."foo_id" FROM "foo_bar" ORDER BY
> "foo_bar"."foo_id" ASC
> }}}
New description:
The documentation on `order_by()` states:
> It is also possible to order a queryset by a related field, without
incurring the cost of a JOIN, by referring to the _id of the related
field:
> {{{
> # No Join
> Entry.objects.order_by('blog_id')
>
> # Join
> Entry.objects.order_by('blog__id')
> }}}
([https://docs.djangoproject.com/en/1.11/ref/models/querysets/#order-by
link])
However, in recent versions the latter query is optimised and won't create
a join:
{{{
>>> print(Bar.objects.order_by('foo__id').query)
SELECT "foo_bar"."id", "foo_bar"."foo_id" FROM "foo_bar" ORDER BY
"foo_bar"."foo_id" ASC
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/28561#comment:1>
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/28561#comment:2>
* has_patch: 0 => 1
Comment:
I found that the behavior changed (removing the join for
`.order_by('blog__id')`) in ccbba98131ace3beb43790c65e8f4eee94e9631c
(Django 1.7). Oddly, the text in question was added in
21b858cb6735cdfdc695ff7b076e4cbc1981bc88 as part of Django 1.8, so it
seems it was inaccurate since its introduction.
Unless there's a subtlety I'm not thinking of, I think we can remove that
text. [https://github.com/django/django/pull/9024 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/28561#comment:3>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"44a6c27fd461e1d2f37388c26c629f8f170e8722" 44a6c27f]:
{{{
#!CommitTicketReference repository=""
revision="44a6c27fd461e1d2f37388c26c629f8f170e8722"
Fixed #28561 -- Removed inaccurate docs about QuerySet.order_by() and
joins.
As of ccbba98131ace3beb43790c65e8f4eee94e9631c, both examples don't use
a join.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28561#comment:4>
Comment (by Tim Graham <timograham@…>):
In [changeset:"a86d95726bdf84efaba7f30c44edddf6c34d2c2e" a86d9572]:
{{{
#!CommitTicketReference repository=""
revision="a86d95726bdf84efaba7f30c44edddf6c34d2c2e"
[1.11.x] Fixed #28561 -- Removed inaccurate docs about QuerySet.order_by()
and joins.
As of ccbba98131ace3beb43790c65e8f4eee94e9631c, both examples don't use
a join.
Backport of 44a6c27fd461e1d2f37388c26c629f8f170e8722 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28561#comment:5>
Comment (by Tim Graham <timograham@…>):
In [changeset:"260bc84901951d9a9f23f23a4d7ae8bd4e2de298" 260bc84]:
{{{
#!CommitTicketReference repository=""
revision="260bc84901951d9a9f23f23a4d7ae8bd4e2de298"
[1.10.x] Fixed #28561 -- Removed inaccurate docs about QuerySet.order_by()
and joins.
As of ccbba98131ace3beb43790c65e8f4eee94e9631c, both examples don't use
a join.
Backport of 44a6c27fd461e1d2f37388c26c629f8f170e8722 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28561#comment:6>
Comment (by Tim Graham <timograham@…>):
In [changeset:"3df8ccf6fc3fa0ab2acf9a03da43fea87f8ff392" 3df8ccf6]:
{{{
#!CommitTicketReference repository=""
revision="3df8ccf6fc3fa0ab2acf9a03da43fea87f8ff392"
[1.9.x] Fixed #28561 -- Removed inaccurate docs about QuerySet.order_by()
and joins.
As of ccbba98131ace3beb43790c65e8f4eee94e9631c, both examples don't use
a join.
Backport of 44a6c27fd461e1d2f37388c26c629f8f170e8722 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28561#comment:7>
Comment (by Tim Graham <timograham@…>):
In [changeset:"e3cf8d2e94fe5f0b6d41774edbab0a9800b397ff" e3cf8d2e]:
{{{
#!CommitTicketReference repository=""
revision="e3cf8d2e94fe5f0b6d41774edbab0a9800b397ff"
[1.8.x] Fixed #28561 -- Removed inaccurate docs about QuerySet.order_by()
and joins.
As of ccbba98131ace3beb43790c65e8f4eee94e9631c, both examples don't use
a join.
Backport of 44a6c27fd461e1d2f37388c26c629f8f170e8722 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28561#comment:8>