django.db.utils.IntegrityError 在自定义用户模型中
django.db.utils.IntegrityError in Custom UserModel
自定义用户模型:
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'
在 运行 ./manage.py migrate
上,我收到以下错误:
django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint')
SHOW ENGINE INNODB STATUS
在 mysql 上显示:
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`)
所以,我删除了整个数据库并重新创建了它,仍然出现同样的错误。
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',
)
您已将 managed
设置为 False
,这意味着 Django 将不会创建 table。删除该行,或手动创建 table。
自定义用户模型:
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'
在 运行 ./manage.py migrate
上,我收到以下错误:
django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint')
SHOW ENGINE INNODB STATUS
在 mysql 上显示:
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`)
所以,我删除了整个数据库并重新创建了它,仍然出现同样的错误。
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',
)
您已将 managed
设置为 False
,这意味着 Django 将不会创建 table。删除该行,或手动创建 table。