Upload to Heroku – ProgrammingError: relation does not exist

Question:

I deployed my application to Heroku, but I am not able to add any data.
When I tried to open first table with data from model I received this error message:

ProgrammingError at /category/category_table
relation "tables_category" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "tables_category"

And when I tried to add first data I received this error message:

ProgrammingError at /admin/tables/category/add/
relation "tables_category" does not exist
LINE 1: INSERT INTO "tables_category" ("category_name") VALUES ('sho...

I went through similar Q/A here, but they do not solve my issue, as I have:

a) deleted all migration files from my local disk, then

b) I run: python3 manage.py makemigrations

c) I run: heroku run python3 manage.py migrate

So all should be up to date and I have this log from Heroku:

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, tables
Running migrations:
  No migrations to apply.

So currently do not know what to do.

Asked By: LadisPavel

||

Answers:

Don’t delete migrations

The first step here is very dangerous:

I have… deleted all migration files from my local disk

It is very likely that the new migrations you generate won’t work on your local machine. They will attempt to migrate from an empty database to whatever your current state is, but you (presumably) already have tables and maybe even data in your local database from earlier migrations.

Deleting migrations is almost never the right answer. I know a lot of people recommend it whenever you get into migration trouble, but they are wrong. Throwing away your database might feel safe during early development, but it definitely isn’t desirable when you have real data later on.

I strongly suggest that you restore your old migration files, then generate new migrations on top of them.

Generating and applying migrations

When you run

python3 manage.py makemigrations

you generate new migration files on your local machine. Heroku has no idea that they exist, so running

heroku run python3 manage.py migrate

won’t do anything.

After cerating migrations on your local machine, apply them locally by running python manage.py migrate to make sure they do what they’re supposed to do. When you’re happy with them, commit the files, redeploy to Heroku, and then run the migrations on Heroku.

Answered By: Chris