Django: DatabaseError column does not exist


I'm having a problem with Django 1.2.4.

Here is a model:

class Foo(models.Model):
    # ...
    ftw = models.CharField(blank=True)
    bar = models.ForeignKey(Bar, blank=True)

Right after flushing the database, I use the shell:

Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from import Foo
>>> Foo.objects.all()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/", line 67, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/", line 82, in __len__
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/", line 271, in iterator
    for row in compiler.results_iter():
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/", line 677, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/", line 732, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python2.6/dist-packages/django/db/backends/", line 15, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.6/dist-packages/django/db/backends/postgresql_psycopg2/", line 44, in execute
    return self.cursor.execute(query, args)
DatabaseError: column foo_foo.bar_id does not exist
LINE 1: ...t_omg", "foo_foo"."ftw", "foo_foo...

What am I doing wrong here?

Update: If I comment out the ForeignKey, the problem disappears.

Update 2: Curiously, this unit test works just fine:

def test_foo(self):
    f = Foo()

    self.assertTrue(f in Foo.objects.all())

Why does it work here but not in the shell?

Update 3: The reason it works in unit testing but not the shell may have something to do with the different databases being used:

    'default': {
        'ENGINE': 'postgresql_psycopg2',
        'NAME': 'foo',
        'USER': 'bar',
        'PASSWORD': 'baz',
        'HOST': '',
        'PORT': '',

import sys
if 'test' in sys.argv or True:
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': 'testdb'

Update 4: Confirmed that when I use SQLite3 as the db, everything works fine.

1/14/2011 3:11:56 AM

Accepted Answer

Try completely dropping/wiping the database before running syncdb.

I remember needing to do that a while back when I had made changes to foreign key fields.

1/16/2011 10:51:55 AM

Please Read this before you drop your entire DB

I had same issue. Please read the exception completely. I had a ModelForm class that read from my table to build a form and exception was there. I commented out and then run the makemigrations and works completely. After that I commented the ModelForm class and everything is working perfect.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow