Django + postgres relation does not exist error

Viewed 26297

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

9 Answers

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.

I was able to solve this issue by the following steps

  1. when I was running this command

    python manage.py migrate app_name zero

    it was complaining about that some table is missing. so I've created a dummy table with a dummy column.

  2. I've run the command again

    python manage.py migrate app_name zero

  3. I've applied the migrations for that app

    python manage.py migrate app_name

What I did for me was to fake migration using the command below

python3 manage.py migrate yourapp --fake

A necro by me, but if you dropped a table on a project with existing data and active migrations (Django, Postgres), your best bet is to drop the entire database (of course proceed with caution), recreate it, and then run manage.py migrate to re-initialize your environment.

It's the fastest way to soft reset and get in sync with the current build.

Finally, it worked for me. I have to delete my all tables and then deleted migrations files.

Then tried the below commands manually :

python manage.py makemigrations [appname]

python manage.py migrate [appname]

finally this worked for me:

python manage.py makemigrations [app_name]

python manage.py migrate [app_name]
Related