Migration error(Migration is applied before its dependency accounts.0001_initial on database 'default')

Question:

I’m trying to migrate my custom user model and I run makemigrations command to make migrations for new models. But when I run migrate command it throws this error :

raise InconsistentMigrationHistory(
django.db.migrations.exceptions.InconsistentMigrationHistory:
Migration admin.0001_initial is applied before its dependency
accounts.0001_initial on database ‘default’.

Trace back:

Traceback (most recent call last):
  File "C:Usersenoshvenv_rulingrulingmanage.py", line 22, in <module>
    main()
  File "C:Usersenoshvenv_rulingrulingmanage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:Usersenoshvenv_rulinglibsite-packagesdjangocoremanagement__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:Usersenoshvenv_rulinglibsite-packagesdjangocoremanagement__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:Usersenoshvenv_rulinglibsite-packagesdjangocoremanagementbase.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:Usersenoshvenv_rulinglibsite-packagesdjangocoremanagementbase.py", line 398, in execute
    output = self.handle(*args, **options)
  File "C:Usersenoshvenv_rulinglibsite-packagesdjangocoremanagementbase.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:Usersenoshvenv_rulinglibsite-packagesdjangocoremanagementcommandsmigrate.py", line 95, in handle
    executor.loader.check_consistent_history(connection)
  File "C:Usersenoshvenv_rulinglibsite-packagesdjangodbmigrationsloader.py", line 306, in check_consistent_history

models.py

from django.contrib.auth.models import AbstractUser


class CustomUser(AbstractUser):
    """extend usermodel"""

    class Meta:
        verbose_name_plural = 'CustomUser'

I just mentioned user model in this question but still if more code is required then tell me I’ll update my question with that information. Thank you

Asked By: daylyroppo2

||

Answers:

  1. Delete the DB
  2. Delete pycache and migrations from all the apps
  3. Make sure you have set AUTH_USER_MODEL
  4. Make migrations
Answered By: Gaurav Rajput

I just thought I might throw in my experience with this – as a Postgres noob.

Using Postgres with the OPTIONS clause in settings.py

DATABASES = {
    'default': {
...
        'OPTIONS': {
            'options': '-c search_path=main,dev'
....
        },

It was not immediately apparent to me that Postgres searches for the table in those schema in order.

So if you are wanting to do a clean, first, initial migration into the ‘main’ schema – be sure there was no previous migration made in schema ‘dev’.

Current current migration in ‘main’ will fail because it finds mis-matched migration data in ‘dev’

Answered By: Derek Smith

Fake migrations on a project can sometimes be troubleshot using –fake

python manage.py migrate --fake
Answered By: armin shoughi