2017-07-08 95 views
0

我無法解決此問題。我已經看到類似的查詢發佈,但沒有任何答案幫助。UNIQUE約束失敗 - Django

我的模型:

class MyUser(AbstractBaseUser, PermissionsMixin): 
    email = models.EmailField(_('email address'), max_length=254, unique=True) 
    contact_number = models.IntegerField(_('contact number'), validators=RegexValidator(r'^\+?1?\d{9,10}$'), unique=True, null=True, blank=True) 
    .... 

在遷移時出現的錯誤:

django.db.utils.IntegrityError: UNIQUE constraint failed: user_profile_myuser.contact_number 

更新:

我回溯:

回溯(最近呼叫最後):

File "manage.py", line 22, in <module> 
    execute_from_command_line(sys.argv) 
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/core/management/__init__.py", line 363, in execute_from_command_line 
    utility.execute() 
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/core/management/__init__.py", line 355, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/core/management/base.py", line 283, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/contrib/auth/management/commands/createsuperuser.py", line 63, in execute 
    return super(Command, self).execute(*args, **options) 
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/core/management/base.py", line 330, in execute 
    output = self.handle(*args, **options) 
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/contrib/auth/management/commands/createsuperuser.py", line 183, in handle 
    self.UserModel._default_manager.db_manager(database).create_superuser(**user_data) 
    File "/home/surajit/website/PubNet/user_profile/manager.py", line 31, in create_superuser 
    **extra_fields) 
    File "/home/surajit/website/PubNet/user_profile/manager.py", line 22, in _create_user 
    user.save(using=self._db) 
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/contrib/auth/base_user.py", line 80, in save 
    super(AbstractBaseUser, self).save(*args, **kwargs) 
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/db/models/base.py", line 806, in save 
    force_update=force_update, update_fields=update_fields) 
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/db/models/base.py", line 836, in save_base 
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) 
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/db/models/base.py", line 922, in _save_table 
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw) 
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/db/models/base.py", line 961, in _do_insert 
    using=using, raw=raw) 
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/db/models/manager.py", line 85, in manager_method 
    return getattr(self.get_queryset(), name)(*args, **kwargs) 
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/db/models/query.py", line 1060, in _insert 
    return query.get_compiler(using=using).execute_sql(return_id) 
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/db/models/sql/compiler.py", line 1099, in execute_sql 
    cursor.execute(sql, params) 
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/db/backends/utils.py", line 80, in execute 
    return super(CursorDebugWrapper, self).execute(sql, params) 
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/db/backends/utils.py", line 65, in execute 
    return self.cursor.execute(sql, params) 
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/db/utils.py", line 94, in __exit__ 
    six.reraise(dj_exc_type, dj_exc_value, traceback) 
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/db/backends/utils.py", line 65, in execute 
    return self.cursor.execute(sql, params) 
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/db/backends/sqlite3/base.py", line 328, in execute 
    return Database.Cursor.execute(self, query, params) 
django.db.utils.IntegrityError: NOT NULL constraint failed: user_profile_myuser.contact_number 

請幫忙。

回答

0

從django.core.validators導入RegexValidator

class MyUser(AbstractBaseUser, PermissionsMixin): 
    phone_regex = RegexValidator(regex=r'^\+?1?\d{9,10}$', message="Phone number must be entered in the format: '+999999999'") 
    phone_number = models.CharField(validators=[phone_regex], blank=True unique=True, null=True, blank=True) 
+0

化妝空和空白假和嘗試,千萬記住makemigratiosn – Exprator

+0

都能跟得上之前刪除舊的文件遷移仍然給出了同樣的錯誤。 – surajitM

+0

是的。現在遷移工作。但是現在我在創建超級用戶時出錯。請檢查回溯。 – surajitM

相關問題