Home > Blockchain >  When we deploy our Django app to Heroku it says "changes that are not yet reflected in a migrat
When we deploy our Django app to Heroku it says "changes that are not yet reflected in a migrat

Time:12-10

My local repo is up-to-date with my Heroku repo.

When I run makemigrations or migrate locally, it says there are no changes.

When I run makemigrations on Heroku, it does these exact same changes every time, no matter how many times I run it:

python manage.py makemigrations users kits email
Migrations for 'kits':
  apps/kits/migrations/0002_auto_20221209_1204.py
    - Change Meta options on historicalkit
    - Alter field history_date on historicalkit
Migrations for 'users':
  apps/users/migrations/0002_auto_20221209_1204.py
    - Change Meta options on historicaluser
    - Alter field history_date on historicaluser

...but then if I run migrate on Heroku, it says there is nothing to migrate, AND that there are un-made migrations:

python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, email, kits, sessions, users
Running migrations:
  No migrations to apply.
  Your models in app(s): 'kits', 'users' have changes that are not yet reflected in a migration, and so won't be applied.
  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.

This is causing our whole web app to go down. What's going on?

CodePudding user response:

It sounds like there is a discrepancy between the migrations on your local machine and the migrations on Heroku. When you run makemigrations on Heroku, it is generating new migrations for the users and kits apps. When you then run migrate on Heroku, it is not applying those migrations because they have not been committed to your codebase.

One possible cause for this issue is that you may not have pushed the latest version of your code to Heroku. When you run makemigrations and migrate locally, they run on the code that is on your local machine. If you have made changes to your code and run makemigrations and migrate locally, but you have not pushed those changes to Heroku, then the code on Heroku will not be up-to-date and the migrations on Heroku will not match the migrations on your local machine.

To fix this issue, you will need to make sure that your code is up-to-date on Heroku. You can do this by pushing your latest code to Heroku using git. For example, you can use the following commands:

git add .
git commit -m "Update code on Heroku"
git push heroku master

Once your code is up-to-date on Heroku, you should be able to run makemigrations and migrate on Heroku without encountering any errors. If your local repository is up-to-date with Heroku, then the issue may be related to the way that Django is handling the migrations. Django maintains a record of which migrations have been applied in the database. If the records in the database do not match the migrations that are present in your code, then you may encounter errors like the ones you are seeing.

One possible solution to this issue is to reset the migration history on Heroku. This will remove the records of the applied migrations from the database, allowing you to run makemigrations and migrate again.

To reset the migration history on Heroku, you can use the following command:

heroku run python manage.py migrate --fake-initial

This will apply the "fake" initial migrations, which will reset the migration history and allow you to run makemigrations and migrate again.

After running this command, you should be able to run makemigrations and migrate on Heroku without encountering any errors. However, keep in mind that this will also remove any data that was added to your database through the migrations, so you may need to restore your data from a backup if you have one available.

CodePudding user response:

When I run makemigrations on Heroku, it does these exact same changes every time, no matter how many times I run it

That makes sense.

When you heroku run something, Heroku spins up a one-off dyno in which to run the command. That dyno has its own ephemeral filesystem that isn't shared with other dynos.

So, every time you run that command it generates new migration files, saving them locally, and then immediately throws them away when the dyno spins down. Running it again gives you a fresh one-off dyno with a fresh filesystem.

Don't ever run makemigrations on Heroku. You should only do that locally.

When I run makemigrations or migrate locally, it says there are no changes

You probably have up to date migration files (so there are none to generate) that aren't committed (so Heroku can't see them), or you're deploying from a branch where those files aren't committed.

Make sure the migration files are properly committed locally, then redeploy.

  • Related