Django: DatabaseError column does not exist

I have a problem with Django 1.2.4.

Here is the model:

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

Immediately after cleaning 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. (InteractiveConsole) >>> from apps.foo.models 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/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... 

What am I doing wrong here?

Refresh . If I comment on ForeignKey , the problem will go away.

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

 def test_foo(self): f = Foo() f.save() 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 various databases used:

settings.py

 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' } } 

Update 4 : it is confirmed that when I use SQLite3 as db everything works fine.

+8
python database django models
source share
5 answers

Try to completely delete / delete the database before running syncdb.

I remember that I needed to do this some time ago when I made changes to the fields of the foreign key.

+9
source share

I fixed this problem by dropping a specific table on this question model. Then used:

 python manage.py syncdb 

If you are using PostgreSQL, I recommend using phppgadmin my web interface, similar to PHPmyadmin, which is used for mySQL.

Alternative to the user interface, you can simply execute the command line

 su postgres #change user to postgres psql <datebase> #access shell for <datebase> database \d #list all tables DROP TABLE "" CASCADE #select a table to drop \q #exit shell 

When in \ d, exit by pressing q.

+3
source share

If you are using Django 1.8, you must create a column. To make sure that you have created the column correctly, find the migration file in which you created the field and run:

 ./manage.py sqlmigrate app_name migration_name_sans_extension 

This will invoke sql commands to create the column. Be sure to include commands that process relationships and run commands in the data console. You won "do something extreme, like dumping a table or database.

+3
source share

Read this before dropping the entire database.

I had the same problem. Please read the exception in its entirety. I had a ModelForm class that I read from my table to create a form, and there was an exception. I commented and then ran makemigrations and worked completely. After that, I commented on the ModelForm class, and everything works fine.

Hope this helps.

+3
source share

I ran into the same problem and noticed that in the database database the fields in which foreign keys are stored does not exist. The problem disappeared after I created the field (which, in my opinion, is peculiar). It seems that Django is not creating a field marked as a foreign key. Any reason why?

0
source share

Source: https://habr.com/ru/post/651052/


All Articles