﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
6373	Ordering by ForeignKey does not work	zavood	nobody	"Prepare a clean Django project:
{{{
$ django-admin.py startproject testsqlite
$ cd testsqlite
$ ./manage.py startapp testapp
}}}

Add the following to `settings.py`:
{{{
import os
PROJDIR = os.path.dirname(os.path.abspath(__file__))
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = PROJDIR + '/test.db'
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',
    'testapp'
)
}}}

Enable admin in `urls.py`:
{{{
#!python
from django.conf.urls.defaults import *

urlpatterns = patterns('',
     (r'^admin/', include('django.contrib.admin.urls')),
)
}}}

Add the following model:
{{{
#!python
from django.db import models

class Foo(models.Model):
    label = models.CharField(max_length=10)
    number = models.PositiveIntegerField(unique=True)
    class Admin:
        pass
    class Meta:
        ordering = ['number']

class Bar(models.Model):
    foo = models.ForeignKey(Foo)
    class Meta:
        ordering = ['foo']
}}}

Load the schema and run server:
{{{
./manage.py syncdb
./manage.py runserver
}}}

 1. go to http://127.0.0.1:8000/admin/
 1. add one Foo object
 1. 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
);
}}}
"		closed	Database layer (models, ORM)	dev		duplicate			Unreviewed	0	0	0	0	0	0
