简体   繁体   中英

Migration fails when extending Django User Model

I'm trying to extend django User model by inheriting AbstractBaseUser so i can be able to manipulate the authentication process of the project.

Here is what my model looks like.

class AccountManager(BaseUserManager):
    ... create_user
    ... create_superuser


class Account(AbstractBaseUser):
    email = models.EmailField(unique=True)
    username = models.CharField(max_length=40, unique=True)

    objects = AccountManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']

And here is my settings INSTALLED_APPS

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'debug_toolbar',
    'rest_framework',
    'compressor',
    'authentication'
]

AUTH_USER_MODEL = 'authentication.Account'

The problem here i notice the migration process that django is bypassing the auth.0001_initial and it jumped directly creating the admin.0001_initial making my migrations to fail with

django.db.utils.IntegrityError: (1215, u'Cannot add foreign key constraint')

How can i fixed this please help?

I was able to solve my issue, by this simple steps:

  1. run python manage.py makemigrations authentication - because when using AUTH_USER_MODEL it will replace the migration of auth_user table of django.contrib.auth altering the migration process. Thus if we fail to provide migration file for authentication app migration will no doubt fails.
  2. run python manage.py migrate .

Binggo!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM