简体   繁体   English

自定义UserModel中的django.db.utils.IntegrityError

[英]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: 在运行./manage.py migrate ,出现以下错误:

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

SHOW ENGINE INNODB STATUS on mysql shows: 在MySQL上显示SHOW ENGINE INNODB STATUS显示:

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: settings.py文件:

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. 您已将managed设置为False ,这意味着Django将不会创建该表。 Either remove that line, or create the table manually. 删除该行,或手动创建表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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