Opened 10 years ago

Closed 10 years ago

#6373 closed (duplicate)

Ordering by ForeignKey does not work

Reported by: zavood Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Prepare a clean Django project:

$ startproject testsqlite
$ cd testsqlite
$ ./ startapp testapp

Add the following to

import os
PROJDIR = os.path.dirname(os.path.abspath(__file__))

Enable admin in

from django.conf.urls.defaults import *

urlpatterns = patterns('',
     (r'^admin/', include('django.contrib.admin.urls')),

Add the following model:

from django.db import models

class Foo(models.Model):
    label = models.CharField(max_length=10)
    number = models.PositiveIntegerField(unique=True)
    class Admin:
    class Meta:
        ordering = ['number']

class Bar(models.Model):
    foo = models.ForeignKey(Foo)
    class Meta:
        ordering = ['foo']

Load the schema and run server:

./ syncdb
./ runserver
  1. go to
  2. add one Foo object
  3. try to add a Bar object, resulting in the following error:
    OperationalError at /admin/testapp/bar/
    no such column: testapp_foo.number

This message is incorrect:

$ sqlite3 test.db 
SQLite version 3.4.2
Enter ".help" for instructions
sqlite> .schema testapp_foo
CREATE TABLE "testapp_foo" (
    "id" integer NOT NULL PRIMARY KEY,
    "label" varchar(10) NOT NULL,
    "number" integer unsigned NOT NULL UNIQUE

Change History (5)

comment:1 Changed 10 years ago by zavood

The problem is that select doesn't use the foreign key table.

Current, erroneous SQL:

SELECT "testapp_bar"."id","testapp_bar"."foo_id" FROM "testapp_bar" ORDER BY "testapp_foo"."id" ASC

Correct SQL:

SELECT "testapp_bar"."id","testapp_bar"."foo_id" FROM "testapp_bar", "testapp_foo" ORDER BY "testapp_foo"."number" ASC;

comment:2 Changed 10 years ago by anonymous

Component: Database wrapperAdmin interface

comment:3 Changed 10 years ago by anonymous

The problem lies in django/contrib/admin/views/, line 739.

For some reason, QuerySet.order_by() does not honour the behaviour documented in , namely:

To order by a field in a different table, add the other table’s name and a dot.

Although the parameter it is called with, qs = qs.order_by('testapp_foo.number'), should be perfectly valid, it produces the error above by not including the testapp_foo table for some reason.

Looks like a serious bug.

comment:4 Changed 10 years ago by anonymous

Component: Admin interfaceDatabase wrapper

comment:5 Changed 10 years ago by Ramiro Morales

Resolution: duplicate
Status: newclosed

Duplicate of #2076 which has been solved on the queryser refactor branch.

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