order_by does not support joins

0 views
Skip to first unread message

dcr...@gmail.com

unread,
Nov 3, 2006, 6:17:21 AM11/3/06
to Django developers
The order_by method does not support joined tables:
.order_by('author__username')

James Bennett

unread,
Nov 4, 2006, 7:02:51 PM11/4/06
to django-d...@googlegroups.com
On 11/3/06, dcr...@gmail.com <dcr...@gmail.com> wrote:
> The order_by method does not support joined tables:
> .order_by('author__username')

Yes, this is ticket #2076: http://code.djangoproject.com/ticket/2076

Until someone figures it out, a workaround would be to do

.extra(tables=['auth_user']).order_by('author__username')

(assuming that your 'author' relation point at auth.User -- the bug is
that Django doesn't pick up all the tables it needs to join, so the
call to 'extra' tells it to bring in auth_user explicitly).

--
"May the forces of evil become confused on the way to your house."
-- George Carlin

Adrian Holovaty

unread,
Nov 4, 2006, 7:04:41 PM11/4/06
to django-d...@googlegroups.com
On 11/3/06, dcr...@gmail.com <dcr...@gmail.com> wrote:
> The order_by method does not support joined tables:
> .order_by('author__username')

Hi dcramer,

This is slightly inelegant, but you can specify the SQL table name in
order_by():

order_by('myapp_authors.username')

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com

serbaut

unread,
Nov 5, 2006, 8:27:47 AM11/5/06
to Django developers

James Bennett

unread,
Nov 5, 2006, 9:00:40 AM11/5/06
to django-d...@googlegroups.com
On 11/5/06, serbaut <ser...@gmail.com> wrote:
> I submitted a patch for this a while back:
> http://code.djangoproject.com/attachment/ticket/2210/order_by_related.diff

Judging from the comments on that ticket, I think it's a slightly
different issue; #2210 deals with fetching the Options class of the
wrong model when the related field has 'db_column' set; in that case,
it appears Django is generating the correct table list for the query,
but not the correct column list. In #2076, which I think is what's
going on here, Django generates the right column list, but has the
wrong table list. I don't think these are related.

serbaut

unread,
Nov 5, 2006, 2:37:21 PM11/5/06
to Django developers
> Judging from the comments on that ticket, I think it's a slightly
> different issue; #2210 deals with fetching the Options class of the
> wrong model when the related field has 'db_column' set; in that case,
> it appears Django is generating the correct table list for the query,
> but not the correct column list. In #2076, which I think is what's
> going on here, Django generates the right column list, but has the
> wrong table list. I don't think these are related.

You may be right, I looked at both tickets when I searched for a fix
before I wrote one myself and I think I submitted the patch on the
wrong ticket. I thought they were related then but maybe not. The patch
in #2210 should be for #2076.

Reply all
Reply to author
Forward
0 new messages