2

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 :

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

Traceback:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\AHMED\anaconda3\envs\dotesrow\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\AHMED\anaconda3\envs\dotesrow\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\AHMED\anaconda3\envs\dotesrow\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\AHMED\anaconda3\envs\dotesrow\lib\site-packages\django\core\management\base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "C:\Users\AHMED\anaconda3\envs\dotesrow\lib\site-packages\django\core\management\base.py", line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\AHMED\anaconda3\envs\dotesrow\lib\site-packages\django\core\management\commands\migrate.py", line 95, in handle
    executor.loader.check_consistent_history(connection)
  File "C:\Users\AHMED\anaconda3\envs\dotesrow\lib\site-packages\django\db\migrations\loader.py", line 302, in check_consistent_history
    raise InconsistentMigrationHistory(
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency accounts.0001_initial on database 
'default'.

models.py

class User(auth.models.AbstractUser):

    current_balance = models.IntegerField(("current balance"),default=0,blank=True)
    age = models.IntegerField(("age"),null=True)
    gender = models.CharField(("gender"), max_length=50,choices=GENDER_CHOICES,null=True)
    nationality = models.CharField(("nationality"), max_length=50,null=True)


    def __str__(self):
        return "@{}".format(self.username)

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ahmed Yasin
  • 886
  • 1
  • 7
  • 31

2 Answers2

3

If you are using a version control tool such as git, just revert changes in migrations. But if not, you may clear migration history as explained here.

It suggests to connect to database using python manage.py dbshell and then view migration history using select * from django_migrations where app='admin';. Then find the id of 0001 migration and delete it like this: delete from django_migrations where id=<some_id>; where some_id is the migration id.

therealak12
  • 1,178
  • 2
  • 12
  • 26
-1

delete all the Migration folders. also delete db.sqlite3, then run python manage.py makemigrations and python manage.py migrate