3

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?

User
  • 23,729
  • 38
  • 124
  • 207

2 Answers2

8

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.

slackmart
  • 4,754
  • 3
  • 25
  • 39
  • 1
    Ouch... `showmigrations` option was introduced in django 1.8. https://docs.djangoproject.com/ja/1.9/ref/django-admin/#django-admin-showmigrations – slackmart May 01 '16 at 20:32
  • 1
    I just jumped to Django 1.9. I'll update the question. Thank-you! – User May 01 '16 at 20:48
0

If you have bad migration, you should simply remove it and generate it again.

GwynBleidD
  • 20,081
  • 5
  • 46
  • 77