Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django + postgres relation does not exist error

I understand how many questions there are for this matter but none of the solutions suggested helped. I got the following problem: I deleted my models and deleted the imports in admin.py as well. Then I wanted to update the django database with makemigrations which shows that it deletes the model employee but then after using migrate command I saw an error that the relation does not exist. I also dropped it from the postgres database that I used. The strange thing here is that migrate keeps looking for the model which does not exists. Even if I create another one makemigrations detects it and then migrate says relation project_employee.. Tried syncdb --all no success. here is the output of migrate

Apply all migrations: sessions, project, admin, auth, contenttypes
Running migrations:
Applying project.0003_auto_20160318_1215...Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "project_employee" does not exist


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
 File "manage.py", line 10, in <module>
  execute_from_command_line(sys.argv)
File "/usr/lib/python3/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/usr/lib/python3/dist-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib/python3/dist-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/lib/python3/dist-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/usr/lib/python3/dist-packages/django/core/management/commands/migrate.py", line 161, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/usr/lib/python3/dist-packages/django/db/migrations/executor.py", line 68, in migrate
self.apply_migration(migration, fake=fake)
File "/usr/lib/python3/dist-packages/django/db/migrations/executor.py", line 102, in apply_migration
migration.apply(project_state, schema_editor)
File "/usr/lib/python3/dist-packages/django/db/migrations/migration.py", line 108, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
 File "/usr/lib/python3/dist-packages/django/db/migrations/operations/fields.py", line 84, in database_forwards
schema_editor.remove_field(from_model, from_model._meta.get_field_by_name(self.name)[0])
 File "/usr/lib/python3/dist-packages/django/db/backends/schema.py", line 431, in remove_field
self.execute(sql)
 File "/usr/lib/python3/dist-packages/django/db/backends/schema.py", line 111, in execute
cursor.execute(sql, params)
 File "/usr/lib/python3/dist-packages/django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
 File "/usr/lib/python3/dist-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
 File "/usr/lib/python3/dist-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
 File "/usr/lib/python3/dist-packages/django/utils/six.py", line 658, in reraise
raise value.with_traceback(tb)
 File "/usr/lib/python3/dist-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
 django.db.utils.ProgrammingError: relation "project_employee" does not exist

`

Thank you

like image 595
Nick Avatar asked Mar 18 '16 15:03

Nick


2 Answers

AFAIK, you should not directly delete a table from the DB before the migration. If you change your model, then manage.py migrate will do the thing.

django cannot detect the direct change of DB; only knows the change of model script. Therefore if you drop a table, then django does not detect the change, so django keeps looking for the table which was dropped and gives the error.

Sometimes migration does not work for no reasons. in that case, I do the following things:

  1. undo the change of models.py
  2. do the django migration ( manage.py makemigrations appname works better than manage.py makemigrations )
  3. if the migration works, then change the models.py again
  4. do the django migration again

this works sometimes.

like image 101
Leonard2 Avatar answered Sep 23 '22 20:09

Leonard2


Drop the tables in the db using the below code

python manage.py migrate app_name zero

Then again migrate

python manage.py migrate app_name

The reason is that there is already a table present, and when you do a "initial migration",Django will see that the initial migration has already been applied since the table is already present with old schema and therefore not taking the new table with different schema into consideration.

like image 28
Shinto Joseph Avatar answered Sep 21 '22 20:09

Shinto Joseph