Opened 2 years ago

Closed 2 years ago

Last modified 2 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 Changed 2 years ago by Marten Kenbeek

Description: modified (diff)

comment:2 Changed 2 years ago by Tim Graham

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 Changed 2 years ago by Tim Graham

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 Changed 2 years ago by GitHub <noreply@…>

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 Changed 2 years ago by Tim Graham <timograham@…>

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 Changed 2 years ago by Tim Graham <timograham@…>

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 Changed 2 years ago by Tim Graham <timograham@…>

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 Changed 2 years ago by Tim Graham <timograham@…>

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