Search code examples
djangodjango-migrationsdjango-1.9

How to redo makemigrations after getting an error?


So first I ran makemigrations and then I ran migrate and got the error ValueError: The database backend does not accept 0 as a value for AutoField.

So I went ahead, modified my models.py to fix that error.

Now, when I run makemigrations to start over again, it works. Then migrate didn't actually do anything but show the error again. So there is a migration there that is bad, then there is one after that is proper.

So I tried running ./manage.py migrate --fake mainapp zero after reading it on Stack Overflow and now it's saying django.db.utils.OperationalError: (1050, "Table 'mainapp_article' already exists").

Any ideas how I can get back to where I started, and retry the makemigrations now with the error removed from models.py?


Solution

  • 1) Identify your last success migration:

    ./manage.py showmigrations mainapp
    [X] 0001_initial
    [X] 0002_auto_20160425_0102
    [X] 0003_auto_20160426_2022
    [X] 0004_auto_20160427_0036
    

    2) Then use migrate to migrate your database to that specified migration point.

     ./manage.py migrate mainapp 0003_auto_20160426_2022
    

    In this example I'm assuming the 0003 migration was success while the 0004 wasn't.

    3) Remove the migration file

    rm mainapp/migrations/0004_auto_20160427_0036.py*
    

    4) Run makemigrations and migrate again.