title: Django: DatabaseError column does not exist
body: <p>I'm having a problem with Django 1.2.4. </p>
  
  <p>Here is a model:</p>
  
  <pre><code>class Foo(models.Model):
      # ...
      ftw = models.CharField(blank=True)
      bar = models.ForeignKey(Bar, blank=True)
  </code></pre>
  
  <p>Right after flushing the database, I use the shell:</p>
  
  <pre><code>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.
  (InteractiveConsole)
  &gt;&gt;&gt; from apps.foo.models import Foo
  &gt;&gt;&gt; Foo.objects.all()
  Traceback (most recent call last):
    File "&lt;console&gt;", line 1, in &lt;module&gt;
    File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 67, in __repr__
      data = list(self[:REPR_OUTPUT_SIZE + 1])
    File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 82, in __len__
      self._result_cache.extend(list(self._iter))
    File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 271, in iterator
      for row in compiler.results_iter():
    File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 677, in results_iter
      for rows in self.execute_sql(MULTI):
    File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 732, in execute_sql
      cursor.execute(sql, params)
    File "/usr/local/lib/python2.6/dist-packages/django/db/backends/util.py", line 15, in execute
      return self.cursor.execute(sql, params)
    File "/usr/local/lib/python2.6/dist-packages/django/db/backends/postgresql_psycopg2/base.py", 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...
  </code></pre>
  
  <p>What am I doing wrong here?</p>
  
  <p><strong>Update</strong>: If I comment out the <code>ForeignKey</code>, the problem disappears.</p>
  
  <p><strong>Update 2</strong>: Curiously, this unit test works just fine:</p>
  
  <pre><code>def test_foo(self):
      f = Foo()
      f.save()
  
      self.assertTrue(f in Foo.objects.all())
  </code></pre>
  
  <p>Why does it work here but not in the shell?</p>
  
  <p><strong>Update 3</strong>: The reason it works in unit testing but not the shell may have something to do with the different databases being used:</p>
  
  <p><strong>settings.py</strong>:</p>
  
  <pre><code>DATABASES = {
      'default': {
          'ENGINE': 'postgresql_psycopg2',
          'NAME': 'foo',
          'USER': 'bar',
          'PASSWORD': 'baz',
          'HOST': '',
          'PORT': '',
      }
  }
  
  import sys
  if 'test' in sys.argv or True:
      DATABASES = {
          'default': {
              'ENGINE': 'django.db.backends.sqlite3',
              'NAME': 'testdb'
          }
      }
  </code></pre>
  
  <p><strong>Update 4</strong>: Confirmed that when I use SQLite3 as the db, everything works fine.</p>