django.db.migrations.exceptions.InconsistentMigrationHistory

后端 未结 25 1025
耶瑟儿~
耶瑟儿~ 2020-12-04 08:16

When I run python manage.py migrate on my Django project, I get the following error:

Traceback (most recent call last):
File \"manage.py\", line         


        
相关标签:
25条回答
  • 2020-12-04 08:57

    Problem

    django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency account.0001_initial on database 'default'.

    So we can migrate database without admin(admin.0001_initial) firstly.

    After its dependency migrated, execute commands to migrate admin.0001_initial.

    Solution

    1. remove 'django.contrib.admin' from INSTALLED_APPS in settings.py.
    2. execute commands:

    Python manage.py makemigrations appname

    Python manage.py migrate appname

    1. add 'django.contrib.admin' to INSTALLED_APPS in settings.py file.
    2. execute commands again:

    Python manage.py makemigrations appname

    Python manage.py migrate appname

    0 讨论(0)
  • 2020-12-04 08:57

    Solved by commenting this before migration in settings.py 'django.contrib.admin' and in urls.py, ('admin/', admin.site.urls). uncomment after migrate

    0 讨论(0)
  • 2020-12-04 08:58

    This Problem will come most of the time if you extend the User Model post initial migration. Because whenever you extend the Abstract user it will create basic fields which were present un the model like email, first_name, etc.

    Even this is applicable to any abstract model in django.

    So a very simple solution for this is either create a new database then apply migrations or delete [You all data will be deleted in this case.] the same database and reapply migrations.

    0 讨论(0)
  • 2020-12-04 09:01

    Before performing any other steps, back up your database. Then back it up again.

    Remove any custom user model code out of the way, disable your custom model and app in settings, then:

    python manage.py dumpdata auth --natural-primary --natural-foreign > auth.json
    python manage.py migrate auth zero  # This will also revert out the admin migrations
    

    Then add in your custom model, set it in settings, and re-enable the app. Make sure you have no migrations on this app yet.

    Then:

    python manage.py makemigrations <your-app>
    python manage.py migrate
    python manage.py loaddata auth.json  # Assumes your user-model isn't TOO dissimilar to the standard one.
    

    Done!

    0 讨论(0)
  • 2020-12-04 09:01

    There is another reason besides user error that can lead to this sort of problem: a known issue with Django when it comes to squashed migrations.

    We have a series of migrations that work perfectly fine in Python 2.7 + Django 1.11. Running makemigrations or migrate always works as it should, etc., even (for the purpose of testing) when the database is freshly re-created.

    However, as we move a project to Python 3.6 (currently using the same Django 1.11) I've been stuck trying to figure out why the same migrations apply just fine only the first time they are run. After that, any attempt to run makemigrations or even just migrate results in the error:

    django.db.migrations.exceptions.InconsistentMigrationHistory
    

    wherein migration foo.0040-thing is applied before its dependency foo.0038-something-squashed-0039-somethingelse (we only happen to have that one squashed migration... the rest are much more straightforward).

    What's bugged me for a while is why this only happens on Python 3. If the DB is truly inconsistent this should be happening all the time. That the migrations appear to work perfectly fine the first time they are applied was equally confounding.

    After much searching (including the present Q&A thread), I stumbled upon the aforementioned Django bug report. Our squash migration did indeed use the b prefix in the replaces line (e.g., replaces = [(b'', 'foo.0038-defunct'),.......]

    Once I removed the b prefixes from the replaces line it all worked normally.

    0 讨论(0)
  • 2020-12-04 09:02

    Your django_migrations table in your database is the cause of inconsistency and deleting all the migrations just from local path won't work.

    You have to truncate the django_migrations table from your database and then try applying the migrations again. It should work but if it does not then run makemigrations again and then migrate.

    Note: don't forget to take a backup of your data.

    0 讨论(0)
提交回复
热议问题