order_by with related table does not work
|Reported by:||Malcolm Tredinnick||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||master|
|Cc:||gary.wilson@…, real.human@…, arthur.case@…, tom@…, mark@…, portland@…, floguy@…, django@…, remco@…, eliott@…, pythonmailing@…||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
This came up on IRC today: if you look at the order_by query that uses the related table Blogs in http://www.djangoproject.com/documentation/db_api/#order-by-fields, it will not actually work. The SQL we produce does not join the blogs_blog table to the blogs_entry table in the SQL query, although it does put in an "order by blogs_blog.name ASC" bit. We need to add in the extra table to the join as well, although this requires reordering the execution in django/db/models/query.py (in the _get_sql_clause() method).
A temporary hack for the moment is
Entry.objects.select_related().order_by('blogs_blog.name', 'headline') but that is a bit clunky.
We need a test for this, too.
(assigning to myself for now; just noting this here in case I get hit by a bus.)
Change History (53)
comment:10 follow-up: 19 Changed 10 years ago by
|Triage Stage:||Unreviewed → Ready for checkin|
comment:15 follow-up: 23 Changed 10 years ago by
|Status:||reopened → new|
|Triage Stage:||Ready for checkin → Accepted|
comment:25 Changed 10 years ago by
|Cc:||mark@… added; gary.wilson@… real.human@… arthur.case@… tom@… removed|