24

I recently added a model to my app (UserProfile) and when I pushed the changes to Heroku, I think I accidentally ran heroku run python manage.py makemigrations. Now when I try to run heroku run python manage.py migrate I get the error below

(leaguemaster) benjamins-mbp-2:leaguemaster Ben$ heroku run python manage.py migrate
Running `python manage.py migrate` attached to terminal... up, run.1357
Operations to perform:
  Synchronize unmigrated apps: allauth
  Apply all migrations: auth, admin, socialaccount, sites, accounts, account, contenttypes, sessions, leagueapp
Synchronizing apps without migrations:
  Creating tables...
  Installing custom SQL...
  Installing indexes...
Running migrations:
  No migrations to apply.
  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.

How do I fix this? Please help!

Ben
  • 20,038
  • 30
  • 112
  • 189
  • I've end up here because I've changed order of INSTALLED_APPS, I guess. I see no other reason, yet – Denis Apr 26 '21 at 14:30

5 Answers5

53

You need to first create the migrations locally, add them to your repository, commit the files with the new migrations and then push to heroku.

The sequence is something like this:

1. (add/modify some someapp/models.py)
2. python manage.py makemigrations someapp
3. python manage.py migrate
4. git add someapp/migrations/*.py (to add the new migration file)
5. git commit -m "added migration for app someapp"
6. git push heroku
7. heroku run python manage.py migrate
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • 1
    First - thanks a bunch for attempting to help me. Now, my local environment is up to date and working as I expect. When I run `git status`, it says my files are up to date and when I run `git push heroku master` it says Everything up-to-date. Yet, when I run `heroku run python manage.py migrate` I get the error "Your models have changes that are not yet reflected in a migration" as I described. Any other ideas or suggestions for me to troubleshoot? – Ben Feb 19 '15 at 05:59
  • It seems then that your repositories are out of sync; do you have any untracked files? – Burhan Khalid Feb 19 '15 at 06:02
  • If you're referring to my .gitignore file, it is a copy of this https://github.com/github/gitignore/blob/master/Python.gitignore with nothing extra – Ben Feb 19 '15 at 06:05
  • 1
    I deleted my app inside heroku, but when I added it back I still get the same error! Any chance I could hire you to help me get this figured out? – Ben Feb 19 '15 at 06:22
  • What do you get with this: `git ls-files . --exclude-standard --others` ? – Burhan Khalid Feb 19 '15 at 06:44
  • This is the last thing I can suggest - you need to check if there is a difference between your Heroku codebase and what you have now; assuming there are no outstanding commits .. try this `git fetch heroku && git log -p master..heroku/master` – Burhan Khalid Feb 19 '15 at 06:53
  • No luck : /. Really appreciate your help though and my offer stands if you're interested. – Ben Feb 19 '15 at 06:57
  • I just noticed that I have two db.sqlite3 files - one in the root of my project, "leaguemaster" and another inside "leaguemaster/leaguemaster". Is this normal, or could this be causing my problem? – Ben Feb 19 '15 at 15:21
  • Update: The error is related to this issue https://github.com/pennersr/django-allauth/issues/836 – Ben Feb 19 '15 at 20:00
10

1. Make migrations locally

$ python manage.py makemigrations && python manage.py migrate

2. Commit changes and push it on the server

$ git add --all
$ git commit -m "Fixed migrate error"
$ git push heroku master

3. Now run migrate on the server

$ heroku run python manage.py migrate

You also need to be sure that you haven't ignored these migration paths in your .gitingnore file

Gedeon Mutshipayi
  • 2,871
  • 3
  • 21
  • 42
  • 2
    You never need to makemigrations on the heroku server. You should always makemigrations locally then push then to the server. you should only have to migrate on the server. – Shahar Gino Jun 03 '21 at 05:44
2

Answered for my case:

your_field=models.CharField(max_length=9,default=False)

Convert to

your_field=models.CharField(max_length=9,default='False')

My case: In models.py for the field I wanted to set the default value to False. First default = False my contract without ''. But after running python manage.py migrate, I got the above error. The problem was solved after placing False inside ''.

Sometimes it is necessary for the default value in our field model to be a False string type.

If

default = False

If you write in the model, you will encounter this error.

In fact, depending on the type of field we have, we can not always set the default value of a field in the model to True or False. Must be converted to string for CharField type.

amir avira
  • 31
  • 4
1

It sounds like you ran makemigrations after you made changes to your model but before you had an initial migration file. Try to revert your app to the state it was before you added the new model and run makemigrations again to create the initial migration. Then add your updates back in and run makemigrations once more. This will create a second migration from your initial data structure to the new updated one. Then try your deployment.

https://docs.djangoproject.com/en/1.7/topics/migrations/#adding-migrations-to-apps

Ryan Allen
  • 5,414
  • 4
  • 26
  • 33
  • Thanks. `allauth.socialaccount` was giving me the error. I ended up dropping it and everything migrated fine after that. Other people have reported the error here https://github.com/pennersr/django-allauth/issues/836 Hoping they come up with a patch. – Ben Feb 19 '15 at 23:34
0

It is a BAD IDEA to migrate to heroku the best practice is always to migrate locally, then push to heroku. But if you find yourself in this mess try reverting to the the initial migrations in heroku 'NOT LOCALLY' i.e run "heroku run manage.py 0001_initial " it works for me atleast