[Django]-Django.db.utils.ProgrammingError: relation does not exist

4👍

After adding changing / adding a new model, always make sure to run python manage.py makemigrations and python manage.py migrate.
If for any reason (migration tree re-arrangement, database failure etc.) something went wrong, you can reverse to a specific migration by doing python manage.py migrate {app_name} {migration_index}.
So what I would suggest in your situation is that you try python manage.py migrate {app_name} zero, and then re-migrate back to the latest version.
You might also need to use --fake.

0👍

Here is a possible workaround: Delete old migrations. Comment out all fields in all your models that relates to Document model and perform makemigrations and migrate to create ‘Document’ table alone. Uncomment fields related to Document in other models and make migrations again.

👤Egor

0👍

I ran into the (seemingly) same problem.
Could not run migrations or anything…

The reason in my case is :

I have 2 Databases (with a DB-Router). In one model of Database2 I use a function for a CHOICES-generation witch uses data from model-X of Database1. I wrote the function after the Creation of Database-1.Model-X.

When I now try to deploy the new migrations on the production system without the new Database-1.Model-X I get the same Error.

I saw 3 possibilities:

  • commenting out the function accessing the not existing Model before applying the migrations (somehow lame and error-prone)
  • move the choices generation away from the model to the form (didn’t changed anything)
  • changing/extending the Database-Router to allow ForeignKey-Relations between databases, using a ForeinKey-Relation to DataBase1 while selecting the needed (unique) field in the ForeignKey definition as to_field="XYZ" so the content of the Database2-Field is filled with the right data. <- This ‘works for me’™

Maybe this is an inspiration to someone with the same or a similar problem.

regards

Karl

👤Karl

Leave a comment