2015-07-12 59 views
1

自定義用戶模型:django.db.utils.IntegrityError在自定義的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', 
) 
+1

你有'managed = False',這意味着Django不會創建表。你確定它存在嗎? – Alasdair

+0

愚蠢的錯誤..您的建議工作! –

回答

2

您已設置managedFalse,這意味着Django會不會創建表。要麼刪除該行,要麼手動創建表。

相關問題