order_by with Foreign Keys

365 views
Skip to first unread message

carole...@gmail.com

unread,
Jan 11, 2007, 9:53:48 AM1/11/07
to Django users
I've seen a few posts that this was a bug where you cannot specify
order_by with a column from a foreign key ...
order_by(foreignkeytable__foreignkeycolumn) because it says table not
found.

Does anyone know if there are plans to fix this...or if there is a work
around? I have several resultsets where I have columns from several
different tables (related by foreignkeys) and I would like to be able
sort on those columns. I had originally just had a javascript function
to do it on my table...but now I've added a paginator into the
mix...and need to actual order the result set.

Russell Keith-Magee

unread,
Jan 12, 2007, 5:10:44 AM1/12/07
to django...@googlegroups.com
On 1/11/07, carole...@gmail.com <carole...@gmail.com> wrote:
>
> I've seen a few posts that this was a bug where you cannot specify
> order_by with a column from a foreign key ...
> order_by(foreignkeytable__foreignkeycolumn) because it says table not
> found.
>
> Does anyone know if there are plans to fix this...or if there is a work
> around?

There are no specific plans that I am aware of (i.e., no schedule that
says 'will be fixed by 25 Jan'). There was talk of a refactoring of
query.py that was to address problems like this one, but the developer
that was championing this cause (Malcolm) has been absent of late.

Obviously, it is a bug, and it would be nice if Django had no bugs,
but the core developers have limited time, and we spend it where we
think it has the most benefit. If someone were to step up and fix this
problem, it would probably find its way into the trunk fairly quickly
(assuming it was a complete patch, of good quality, with unit tests,
etc).

Yours,
Russ Magee %-)

gordyt

unread,
Jan 12, 2007, 9:27:57 PM1/12/07
to Django users
Hi Carole,

There is a workaround for this problem. I am using the latest
subversion build of django, so I don't know if it works with the last
official build or not.

Here is an example:

ProductVersion.objects.select_related().order_by("kindledb_product.name","version_number")

Here are the model definitions:

class ProductVersion(models.Model):
product=models.ForeignKey(Product)
version_number=models.CharField(maxlength=16)
availability_date=models.DateField(null=True,blank=True)
release_notes=models.TextField(blank=True)

class Product(models.Model):
name=models.CharField(maxlength=128)


Note that kindledb_product is the name of the database table that
stores the information from the Product.

--gordon

Honza Král

unread,
Jan 12, 2007, 10:46:41 PM1/12/07
to django...@googlegroups.com
this works fine, but if you forget the select_related() it will result
in a cross join which is probably the last thing you want from your
database...

I was bitten by this when I specified this sort of ordering in the model...


--
Honza Král
E-Mail: Honza...@gmail.com
ICQ#: 107471613
Phone: +420 606 678585

DavidA

unread,
Jan 12, 2007, 10:52:18 PM1/12/07
to Django users
I found this patch which fixes it. I've been using it for a while now:
http://code.djangoproject.com/ticket/2210

carole...@gmail.com

unread,
Jan 15, 2007, 1:22:52 PM1/15/07
to Django users
I'll give this a shot...I'm actually using the multi-db branch..but
hopefully this will work here ;)

carole...@gmail.com

unread,
Jan 15, 2007, 2:06:30 PM1/15/07
to Django users
Hmm... the site hangs forever in Apache 2.x when I try this...I've
emailed the branch developer to ask if there is a known issue w/
select_related... hopefully I can get it worked out ;)

Reply all
Reply to author
Forward
0 new messages