导入用户(密钥(电子邮件)=()已存在)

时间:2014-09-01 14:16:58

标签: django postgresql django-allauth

我试图从cvs文件导入用户。有些没有电子邮件,这导致了一些问题。

我正在使用Django 1.6和标准用户。

这是我在尝试导入没有电子邮件的用户时遇到的错误:

IntegrityError('duplicate key value violates unique constraint "account_emailaddress_email_key"\nDETAIL: Key (email)=() already exists.\n',)

Traceback (most recent call last):
  File "/home/<USER>/.virtualenvs/<SITE>/local/lib/python2.7/site-packages/celery/app/trace.py", line 240, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/home/<USER>/.virtualenvs/<SITE>/local/lib/python2.7/site-packages/celery/app/trace.py", line 437, in __protected_call__
    return self.run(*args, **kwargs)
  File "/home/<USER>/.virtualenvs/<SITE>/<PROJECT>/apps/accounts/tasks.py", line 1151, in import_users_from_cvs
  File "/home/<USER>/.virtualenvs/<SITE>/local/lib/python2.7/site-packages/django/db/models/manager.py", line 154, in get_or_create
    return self.get_queryset().get_or_create(**kwargs)
  File "/home/<USER>/.virtualenvs/<SITE>/local/lib/python2.7/site-packages/django/db/models/query.py", line 391, in get_or_create
    six.reraise(*exc_info)
  File "/home/<USER>/.virtualenvs/<SITE>/local/lib/python2.7/site-packages/django/db/models/query.py", line 383, in get_or_create
    obj.save(force_insert=True, using=self.db)
  File "/home/<USER>/.virtualenvs/<SITE>/local/lib/python2.7/site-packages/django/db/models/base.py", line 545, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/<USER>/.virtualenvs/<SITE>/local/lib/python2.7/site-packages/django/db/models/base.py", line 573, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/home/<USER>/.virtualenvs/<SITE>/local/lib/python2.7/site-packages/django/db/models/base.py", line 654, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/home/<USER>/.virtualenvs/<SITE>/local/lib/python2.7/site-packages/django/db/models/base.py", line 687, in _do_insert
    using=using, raw=raw)
  File "/home/<USER>/.virtualenvs/<SITE>/local/lib/python2.7/site-packages/django/db/models/manager.py", line 232, in _insert
    return insert_query(self.model, objs, fields, **kwargs)
  File "/home/<USER>/.virtualenvs/<SITE>/local/lib/python2.7/site-packages/django/db/models/query.py", line 1514, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/<USER>/.virtualenvs/<SITE>/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 903, in execute_sql
    cursor.execute(sql, params)
  File "/home/<USER>/.virtualenvs/<SITE>/local/lib/python2.7/site-packages/django/db/backends/util.py", line 69, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/<USER>/.virtualenvs/<SITE>/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/home/<USER>/.virtualenvs/<SITE>/local/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/<USER>/.virtualenvs/<SITE>/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
IntegrityError: duplicate key value violates unique constraint "account_emailaddress_email_key"
DETAIL:  Key (email)=() already exists.

这是db结构:

test_db=# \d+ account_emailaddress;
                                              Table "public.account_emailaddress"
  Column  |         Type          |                             Modifiers                             | Storage  | Description 
----------+-----------------------+-------------------------------------------------------------------+----------+-------------
 id       | integer               | not null default nextval('account_emailaddress_id_seq'::regclass) | plain    | 
 user_id  | integer               | not null                                                          | plain    | 
 email    | character varying(75) | not null                                                          | extended | 
 verified | boolean               | not null                                                          | plain    | 
 primary  | boolean               | not null                                                          | plain    | 
Indexes:
    "account_emailaddress_pkey" PRIMARY KEY, btree (id)
    "account_emailaddress_email_key" UNIQUE CONSTRAINT, btree (email)
    "account_emailaddress_email_like" btree (email varchar_pattern_ops)
    "account_emailaddress_user_id" btree (user_id)
Foreign-key constraints:
    "account_emailaddress_user_id_fkey" FOREIGN KEY (user_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY DEFERRED
Referenced by:
    TABLE "account_emailconfirmation" CONSTRAINT "account_emailconfirmation_email_address_id_fkey" FOREIGN KEY (email_address_id) REFERENCES account_emailaddress(id) DEFERRABLE INITIALLY DEFERRED
Has OIDs: no

我能做些什么来解决这个问题吗?

编辑:

我可以为每个用户生成一个随机的电子邮件地址,但我仍然会收到错误

IntegrityError('duplicate key value violates unique constraint "account_emailaddress_email_key"\nDETAIL: Key (email)=() already exists.\n',)

有没有办法“重启”account_emailaddress_email_key

2 个答案:

答案 0 :(得分:0)

您可以创建CustomUser类:https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#django.contrib.auth.models.CustomUser.USERNAME_FIELD

然后将USERNAME_FIELD更改为必须在用户之间唯一的字段,在您的情况下&#34;电子邮件&#34;不是其中之一:)

答案 1 :(得分:0)

您似乎没有使用标准的Django auth.User存储这些电子邮件地址(The email field of the standard django.contrib.auth.User is optional)。 您使用分隔的表来存储用户的电子邮件(account_emailaddress)。

当处理过的用户没有电子邮件地址时,您不应尝试添加电子邮件条目(在此account_emailaddress表中)。由于account_emailaddress.email列上的unicity约束,Postgres会为您尝试创建的第二个空电子邮件发回IntegrityError。