简体   繁体   中英

django.db.utils.IntegrityError in Custom UserModel

Custom User Model:

class User(AbstractBaseUser):
    id = models.AutoField(primary_key=True)
    username = models.CharField(max_length=30)
    email = models.EmailField(unique=True,max_length=75)
    test = models.CharField(max_length=20)

    USERNAME_FIELD = 'email'

    class Meta:
        managed = False
        db_table = 'auth_user'

On running ./manage.py migrate , I get the following error:

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

SHOW ENGINE INNODB STATUS on mysql shows:

2015-07-12 19:29:25 133f4a000 Error in foreign key constraint of table edutraffic/#sql-55ea_17a:
 FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`):
Cannot resolve table name close to:
 (`id`)

So, I deleted the complete database and recreated it, still getting the same error.

settings.py file:

AUTH_USER_MODEL = 'users.User'
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'rest_framework',
    'rest_framework.authtoken',
    'rest_auth',
    'allauth',
    'allauth.account',
    'rest_auth.registration',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.facebook',
    'allauth.socialaccount.providers.google',

    'users',
)

You have set managed to False , which means that Django will not create the table. Either remove that line, or create the table manually.

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