Key (email)=() already exists error: python-social-auth

Key (email)=() already exists error: python-social-auth

我的应用程序同时使用电子邮件注册和 Facebook 或 Google+ 使用 python-social-auth (0.2.11) 注册。当 一些 新用户使用 Facebook 注册时,他们会被带到一个错误页面。之后,我看到以下错误:

Internal Server Error: /complete/facebook/
Traceback (most recent call last):
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/newrelic/hooks/framework_django.py", line 527, in wrapper
    return wrapped(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/apps/django_app/utils.py", line 51, in wrapper
    return func(request, backend, *args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/apps/django_app/views.py", line 28, in complete
    redirect_name=REDIRECT_FIELD_NAME, *args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/actions.py", line 43, in do_complete
    user = backend.complete(user=user, *args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/backends/base.py", line 41, in complete
    return self.auth_complete(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/utils.py", line 229, in wrapper
    return func(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/backends/facebook.py", line 87, in auth_complete
    return self.do_auth(access_token, response, *args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/backends/facebook.py", line 119, in do_auth
    return self.strategy.authenticate(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/strategies/django_strategy.py", line 96, in authenticate
    return authenticate(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 74, in authenticate
    user = backend.authenticate(**credentials)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/backends/base.py", line 82, in authenticate
    return self.pipeline(pipeline, *args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/backends/base.py", line 85, in pipeline
    out = self.run_pipeline(pipeline, pipeline_index, *args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/backends/base.py", line 112, in run_pipeline
    result = func(*args, **out) or {}
  File "/app/.heroku/python/lib/python2.7/site-packages/social/pipeline/user.py", line 70, in create_user
    'user': strategy.create_user(**fields)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/strategies/base.py", line 53, in create_user
    return self.storage.user.create_user(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/storage/django_orm.py", line 60, in create_user
    return cls.user_model().objects.create_user(*args, **kwargs)
  File "/app/wehealth/users/models.py", line 47, in create_user
    user.save(using=self._db)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/base.py", line 710, in save
    force_update=force_update, update_fields=update_fields)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/base.py", line 738, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/base.py", line 822, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/base.py", line 861, in _do_insert
    using=using, raw=raw)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py", line 920, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 963, in execute_sql
    cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python2.7/site-packages/newrelic/hooks/database_dbapi2.py", line 25, in execute
    *args, **kwargs)
IntegrityError: duplicate key value violates unique constraint "users_user_email_key"
DETAIL:  Key (email)=() already exists.

我没有在所有新用户中看到此错误。例如,在过去 24 小时内,我在以下时间收到此错误:

但是,我曾让用户在以下时间通过 Facebook 成功注册:

我看不出注册成功的用户和注册失败的用户之间有什么关系。我不认为这是现有用户尝试使用 Facebook 注册的问题。一些第一次使用的用户通知我他们遇到了错误。

看来错误发生在 create_user 函数中。我的流水线如下:

SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.social_details',
    'social.pipeline.social_auth.social_uid',
    'social.pipeline.social_auth.auth_allowed',
    'social.pipeline.social_auth.social_user',
    'social.pipeline.user.get_username',
    'social.pipeline.mail.mail_validation',
    'social.pipeline.social_auth.associate_by_email',
    'social.pipeline.user.create_user',
    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details',
    'users.pipeline.user.store_gender',
    'users.pipeline.user.store_registration_type',
)

知道哪里出了问题吗? Facebook 注册在大约 70% 的时间里对我有用,但在剩下的 30% 的时间里我遇到了这个错误。似乎没有错误的模式,这真的让我陷入困境。我应该迁移到不同的身份验证服务吗?我正在使用 Django 1.8 和 Python 2.7.

问题是您的 email 字段是唯一的,并且您已经有一个具有 '' 值的用户,这是一个空字符串。

发生的事情是,当用户尝试使用 Facebook 登录时,有时 email 是空的(也许他没有授予从 Facebook 获取电子邮件的权限)。然后,当你试图保存它时,它给出了这个错误。

要验证这一点,获取电子邮件为空的用户并将其删除:

user = User.objects.get(email='')
user.delete()

然后,再次尝试使用 Facebook 登录。不应该有任何错误。 现在再试一次。您将遇到同样的错误。

希望对您有所帮助!