django.db.migrations.exceptions.InconsistentMigrationHistory

后端 未结 25 1023
耶瑟儿~
耶瑟儿~ 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:50

    The order of INSTALLED_APPS seems important. If you always put your recent works on top/beginning of the list they'll always be loaded properly in regard to django.contrib.admin. Moving my works to the beginning of the INSTALLED_APPS list fixed this problem for me. The reason Kun Shi's solution may have worked maybe it ran the migrations in a different order.

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

    when you create a new Django project and run

    python manage.py migrate

    The Django will create 10 tables for you by default including one auth_user table and two start with auth_user.

    when you want to create a custom user model inherit from AbstractUser, you will encounter this problem with error message as follow:

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

    I solve this problem by dropping my entire database, and create a new one. And this replaced the three tables I mentioned.

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

    If you set AUTH_USER_MODEL in settings.py like this:

    AUTH_USER_MODEL = 'custom_user_app_name.User'
    

    you should comment this line before run makemigration and migrate commands. Then you can uncomment this line again.

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

    Here how to solve this properly.

    Follow these steps in your migrations folder inside the project:

    1. Delete the _pycache_ and the 0001_initial files.
    2. Delete the db.sqlite3 from the root directory (be careful all your data will go away).
    3. on the terminal run:
        python manage.py makemigrations
        python manage.py migrate

    Voila.

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

    just delete the sqlite file or run flush the databse 'python manage.py flush' and then run makemigrations and migrate commands respectively.

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

    If you are working on an empty database a quick fix could be running the migrations for the account app, before any other app migrations.

    $ ./manage.py migrate account
    

    And then:

    $ ./manage.py migrate
    
    0 讨论(0)
提交回复
热议问题