Django.db.migrations.exceptions.InconsistentMigrationHistory

When i run

python manage.py migrate 

in my django project, I get the following error

 Traceback (most recent call last): File "manage.py", line 22, in <module> execute_from_command_line(sys.argv) File "/home/hari/project/env/local/lib/python2.7/site- packages/django/core/management/__init__.py", line 363, in execute_from_command_line utility.execute() File "/home/hari/project/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 355, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/hari/project/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv self.execute(*args, **cmd_options) File "/home/hari/project/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute output = self.handle(*args, **options) File "/home/hari/project/env/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 86, in handle executor.loader.check_consistent_history(connection) File "/home/hari/project/env/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 298, in check_consistent_history connection.alias, django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency account.0001_initial on database 'default'. 

I have a user model as shown below

 class User(AbstractUser): place = models.CharField(max_length=64, null=True, blank=True) address = models.CharField(max_length=128, null=True, blank=True) 

So how can I solve this?

+37
python django django-models django-rest-framework database-migration
source share
18 answers

Your django_migrations table in your database is causing inconsistency, and removing all migrations from the local path only will not work.

You should truncate the django_migrations table from your database, and then try applying the migrations again. It should work, but if it does not start makemigrations again, then it migrates.

Note. Remember to take a backup of your data.

+22
source share

First of all, delete all tables from the database, delete all files from the migration folder except __init__.py , then run migrate.

+1
source share

Since you are using a user model of the user, you can comment first

 INSTALLED_APPS = [ ... #'django.contrib.admin', ... ] 

in the settings of Installed_Apps. Then run

 python manage.py migrate. 

When finish uncommenting

 'django.contrib.admin'. 
+59
source share

Let's start by solving the problem with most of the answers on this page:

You will never have to delete your database if you use the Django migration system correctly and you should never delete migrations after they are completed

Now, the best solution for you depends on a number of factors that include your experience with Django, the level of your understanding of the migration system, and the value of the data in your database.

In short, there are two ways to resolve any migration error.

  1. Take the nuclear option. Warning: this is only an option if you work alone. If other people depend on existing migrations, you cannot simply remove them.

    • Delete all your migrations and restore the new set with python3 -m manage makemigrations . This should fix any dependency problems or inconsistencies in your migrations.
    • Leave your entire database. This will fix all the problems that you had with inconsistencies between your actual database schema and the scheme that you should have based on the migration history, and eliminate all the problems that you had with inconsistencies between your migration history and your previous migration files [ this is what InconsistentMigrationHistory complains about].
    • Re-create the database schema with python3 -m manage migrate
  2. Determine the cause of the error and eliminate it, because (judging by experience) the reason is almost certainly something stupid that you did. (As a rule, due to a misunderstanding of how to use the migration system correctly). Based on the error I caused, there are three categories.

    1. Inconsistencies with migration files. This is a fairly common case when several people work on a project. We hope that your changes do not conflict, and makemigrations --merge can solve this problem, otherwise someone will have to roll back their migrations to the branch point to solve this problem.
    2. Inconsistencies between your pattern and migration history. To do this, someone had to edit the database schema manually or delete the migrations. If they removed the migration, discard your changes and shout at them; You should never remove migrations unless others depend on them. If they edited the database schema manually, discard their changes and then shout at them; Django manages the database schema, no one else.
    3. Inconsistencies between your migration history and your migration files. [This is the InconsistentMigrationHistory problem that the questioner suffers from and the one I suffered from when I got to this page]. To deal with this, someone either manually ruined the django_migrations table, or deleted the migration after applying it. To solve this problem, you will need to find out how the discrepancy occurred and manually fix it. If your database schema is correct and this is just your migration history, you can manually edit the django_migrations table to solve this problem. If your database schema is incorrect, you will also have to manually edit it to bring it into line with what it should be.

Based on your description of the problem and the answer you choose, I assume that you are working alone, new to Django, and you do not care about your data. Thus, the nuclear option may be right for you.

If you are not in this situation, and the above text looks like nonsense, then I suggest contacting the Django user mailing list for help. There are some very helpful people out there who can help you sort out the particular mess you are in.

Believe me, you can fix this mistake without becoming nuclear!

+31
source share

Here's how to solve it right.

Follow these steps in the migration folder inside the project:

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

Voila.

+15
source share

problem

django.db.migrations.exceptions.InconsistentMigrationHistory: The admin.0001_initial migration is applied before its dependency account 0001_initial for the default database.

Thus, we can first transfer the database without an administrator (admin.0001_initial).

After the dependency migration, run the admin.0001_initial migration admin.0001_initial .

Decision

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

Python manage.py makemigrations application name

Python manage.py transfer application name

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

$: Python manage.py makemigrations application name

$: Python manage.py transfer application name

+9
source share

This happened to me in a new project after I added the User user model as recommended in django docs.

If you are starting a new project, it is highly recommended that you customize your user model, even if the default user model is enough for you.

Here is what I did to solve the problem.

  1. Delete the db.sqlite3 database.
  2. Delete the application / migration folder.

For @jackson, temporarily comment out django.contrib.admin.

 INSTALLED_APPS = [ ... #'django.contrib.admin, ... ] 

Also comment out the site administrator in urls.py:

 urlpatterns = [ path('profile/', include('restapp.urls')), #path('admin/', admin.site.urls), ] 

If you do not comment out the path ('admin /'), you will receive the error message “LookupError: the application with the label“ admin ”was not installed at startup

 python manage.py migrate 

After the migration is completed, uncomment both of the above options.

+3
source share

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

+2
source share

when you create a new Django project and run

Python manage.py migrate

Django will create 10 default tables for you, including one auth_user table and two starting with auth_user.

when you want to create a custom model inherited from AbstractUser, you will encounter this problem with an error message as follows:

 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 creating a new one. And that replaced the three tables that I mentioned.

+2
source share

If you are working with an empty database, a quick solution might be to start migrations for the application account before any other application migrations.

 $ ./manage.py migrate account 

And then:

 $ ./manage.py migrate 
+2
source share

If you set AUTH_USER_MODE L to settings.py as follows:

 AUTH_USER_MODEL = 'custom_user_app_name.User' 

You should comment on this line before running the makemigration and migrate commands. Then you can uncomment this line again.

+1
source share

when you create a new project without applications, you start

 python manage.py migrate 

Django will create 10 tables by default.

If you want to create a custom client model that inherits from AbstractUser after this, you will run into this problem, as shown in the following message:

django.db.migrations.exceptions.InconsistentMigrationHistory: The admin.0001_initial migration is applied before its dependency account 0001_initial for the default database.

finally i dump all the databases and run

+1
source share

I came across this when switching from Wagtail 2.0 to 2.4, but I saw it several times when a third-party application suppresses migration after your current version, but before the version you are switching to.

A shockingly simple solution in this case, at least:

 ./manage.py migrate ./manage.py makemigrations ./manage.py migrate 

those. start one migration before trying to migrate.

+1
source share

First delete all the migration files and db.sqlite3 and follow these steps:

 $ ./manage.py makemigrations myapp $ ./manage.py squashmigrations myapp 0001(may be differ) 

Delete the old migration file and finally

 $ ./manage.py migrate 
0
source share

If this exception occurs when you try to create your own user model instead of the standard one, follow this instruction.
I found a solution to my problem by following this step by step instruction:

  1. Create a user model identical to auth.User, name it User (so many tables have the same name) and set db_table = 'auth_user' (so that it uses the same table).
  2. Throw away all your migrations
  3. Recreate a fresh set of migrations
  4. Donate a chicken, maybe two, if you are worried; also backup your database
  5. Trim the django_migrations table
  6. Fake - apply a new set of migrations
  7. Disable db_table, make other changes to the user model, generate migrations, apply them

It is highly recommended that you do this in a database that provides foreign key constraints. Do not try to use SQLite on your laptop and expect it to work on Postgres on servers!

0
source share

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

We have a number of migrations that work great in Python 2.7 + Django 1.11. Running makemigrations or migrate always works makemigrations , etc., even (for testing purposes) when the database is recreated.

However, when we port the project to Python 3.6 (currently using the same Django 1.11), I got stuck trying to figure out why the same migrations apply normally only on first run. After that, any attempt to start makemigrations or even just migrate results in an error:

 django.db.migrations.exceptions.InconsistentMigrationHistory 

where the migration foo.0040-thing is applied before its dependency foo.0038-something-squashed-0039-somethingelse (we only have one squash migration ... the rest is much simpler).

For a while, I was worried about why this only happens in Python 3. If the database is really incompatible, this should happen all the time. The fact that migrations seem to work just fine the first time they were applied was equally embarrassing.

After a long search (including the current Q&A thread), I came across the aforementioned bug report in Django . Our zucchini migration really use the b prefix in the replaces line (for example, replaces = [(b'', 'foo.0038-defunct'),.......]

After I removed the b prefixes from the replaces line everything worked fine.

0
source share

Your mistake is essentially:

 Migration "B" is applied before its dependency "A" on database 'default'. 

Health Check : First open your database and look at the entries in the django_migrations table. Entries should be listed in chronological order (for example: A, B, C, D ...).

Verify that the migration name "A" specified in the error matches the migration name "A" specified in the database. (They may differ if you previously manually edited or deleted or renamed the migration files)

To fix this , rename migration A. either in the database, or rename the file name. BUT make sure that the changes coincide with what other developers in your team have in their databases (or coincide with the changes in your production database)

0
source share

This problem will occur in most cases if you extend the user model after the initial migration. Because whenever you extend the Abstract user, he creates the base fields that were present in the model, such as email, first_name, etc.

Even this applies to any abstract model in Django.

Thus, a very simple solution for this is to either create a new database, then apply the migration, or delete [In this case, all data will be deleted.] The same database and re-apply the migration.

0
source share

All Articles