Suganesh Kumar
Suganesh Kumar

Reputation: 167

Migrate Issue on Heroku

I have hosted the django application in Heroku environmnet. Whenever I try to initiate migrate command its shows error:

Your models 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. Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.

I also checked with new database on same application eventhough same issue. Finally I have tried makemigrations its done well, but after makemigrations also having same issue. The same I have tried with another application in same heroku makemigrations and migrate working well.

Please suggest why I can't migrate on previous application in heroku.

enter image description here

Upvotes: 1

Views: 1801

Answers (2)

codeSapience
codeSapience

Reputation: 40

I had the same issue so in resolving it I removed /migrations dir in .gitignore for the main/master branch (or your own deployment branch). While every other developer's branch has /migrations dir in their .gitignore - so their personal migrations don't get tracked in git.

That way, there'll be minimal chance of having a merge conflict in migrations files, since no developer's migration is tracked in git.

Finally, remember to;

  1. git switch to main/master (or your deployment branch) locally (git switch main)
  2. pull the latest merged code from origin (git pull)
  3. run your manage.py makemigrations (python ./manage.py makemigrations)
  4. now there should be new/updated migrations locally, add them to your git (git add . ) then git commit your new changes and push to origin (git push)
  5. Manually push to heroku; (git push heroku main:main)...edit accordingly if your deployment branch isn't set to main.
  6. Go into your dyno console (heroku run bash --app ) and apply the migrations (python manage.py migrate)

Upvotes: 0

Pradip Kachhadiya
Pradip Kachhadiya

Reputation: 2235

Here two way to solve this problem:

  1. Don't run makemigrations command in Heroku bash. Run makemigrations locally to create migration files and Run migrate locally then commit the result and push, and then run migrate command on Heroku.

OR

  1. If your existing database have not contain any useful data then destroy it :-

Go to heroku >> your app >> Resources >> select Heroku Postgres >> Settings >> select Destroy Database..

After this follow again first way.

Upvotes: 2

Related Questions