0

I'm trying to creating a custom user in django with AbstractUser class.

code of models.py where i defined my custom_user.

from django.db import models
from django.contrib.auth.models import AbstractUser

class MyUser(AbstractUser):
    country=models.CharField(max_length=20)

i want only country as a extra field with users.That's why i defined only country filed.

i have already mention my custom user model in settings.py.

AUTH_USER_MODEL = 'new_user.MyUser'

when i run migration command it run successfully but when i run migrate it giving me error.like this.

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Futuresoft\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Futuresoft\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Futuresoft\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Futuresoft\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Futuresoft\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 83, in
wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\Futuresoft\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\commands\migrate.py",
line 90, in handle
    executor.loader.check_consistent_history(connection)
  File "C:\Users\Futuresoft\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\loader.py", line 299, in check_consistent_history
    connection.alias,
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency new_user.0001_initial on database 'default'.

i have no idea what is this.Can anyone help with this.

NOTE: I'm trying create custom user after creating my SuperUser and i have already migrate all migrations that comes when we first create our new project.

Maninder Singh
  • 425
  • 6
  • 14
  • Have you tried running migrate on an empty database? I guess you already ran `migrate` and after that you created your custom user. – wfehr Apr 23 '20 at 10:47
  • @wfehr yes how i can fix that now. – Maninder Singh Apr 23 '20 at 10:57
  • Since you are still developing and it's no live-system -> drop your database and set it up new and then run the migrations. – wfehr Apr 23 '20 at 11:02
  • @wfehr but how i can drop my database.**flush** command only clearing table not deleting them.same problem is again facing.How to drop database in django. – Maninder Singh Apr 23 '20 at 17:21

1 Answers1

2

The problem is that the database already has migrations and data in it.

So after migrating the default user model you changed AUTH_USER_MODEL to a custom model. When you are running the migrations then it throws the given error.

Since you are still in development and it's not a production system, the easiest way to get around this is by clearing your database and running migrations again.

Also see docs for this case. There is also a topic on how to create a custom user mid-project.

wfehr
  • 2,185
  • 10
  • 21