Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#28561 closed Cleanup/optimization (fixed)

Docs inaccurate about QuerySet.order_by('foo__id') creating a join

Reported by: Marten Kenbeek Owned by: nobody
Component: Documentation Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Marten Kenbeek)

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')

(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

Change History (8)

comment:1 by Marten Kenbeek, 7 years ago

Description: modified (diff)

comment:2 by Tim Graham, 7 years ago

Summary: .order_by('foo__id') doesn't create a joinDocs inaccurate about QuerySet.order_by('foo__id') creating a join
Triage Stage: UnreviewedAccepted

comment:3 by Tim Graham, 7 years ago

Has patch: set

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

comment:4 by GitHub <noreply@…>, 7 years ago

Resolution: fixed
Status: newclosed

In 44a6c27f:

Fixed #28561 -- Removed inaccurate docs about QuerySet.order_by() and joins.

As of ccbba98131ace3beb43790c65e8f4eee94e9631c, both examples don't use
a join.

comment:5 by Tim Graham <timograham@…>, 7 years ago

In a86d9572:

[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

comment:6 by Tim Graham <timograham@…>, 7 years ago

In 260bc84:

[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

comment:7 by Tim Graham <timograham@…>, 7 years ago

In 3df8ccf6:

[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

comment:8 by Tim Graham <timograham@…>, 7 years ago

In e3cf8d2e:

[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

Note: See TracTickets for help on using tickets.
Back to Top