Django的ignore_conflicts不适用于用户模型

时间:2019-10-15 15:40:54

标签: python django

我正在尝试创建许多用户来测试我的Django应用程序。我已经在其他模型上使用了这种方法,并且可以使用,但是在这里却没有用。执行后,将显示以下错误。使用了最新的Django版本(2.2.6)。 The documentation states

  

(...)将ignore_conflicts参数设置为True会告诉数据库忽略插入任何失败约束的行的失败,例如重复的唯一值。

我的代码:

def draftUser(i):
    return User(username='Testguy' + str(i), first_name=randomString(7), last_name=randomString(5))

user_cache = []

for i in range(1, 10000):
    user_cache(draftUser(i))

User.objects.bulk_create(user_cache, ignore_conflicts=True)

完整错误回溯:

Traceback (most recent call last):
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/models/query.py", line 538, in get_or_create
    return self.get(**kwargs), False
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/models/query.py", line 408, in get
    self.model._meta.object_name
django.contrib.auth.models.DoesNotExist: User matching query does not exist.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: UNIQUE constraint failed: auth_user.username

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "PROJECT_PATH/src/populate.py", line 261, in <module>
    olaf, created = User.objects.get_or_create(username='Olaf', first_name='Olaf', last_name='Dude')
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/models/query.py", line 541, in get_or_create
    return self._create_object_from_params(kwargs, params)
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/models/query.py", line 583, in _create_object_from_params
    raise e
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/models/query.py", line 575, in _create_object_from_params
    obj = self.create(**params)
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/models/query.py", line 422, in create
    obj.save(force_insert=True, using=self.db)
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 66, in save
    super().save(*args, **kwargs)
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/models/base.py", line 741, in save
    force_update=force_update, update_fields=update_fields)
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/models/base.py", line 779, in save_base
    force_update, using, update_fields,
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/models/base.py", line 870, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/models/base.py", line 908, in _do_insert
    using=using, raw=raw)
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/models/query.py", line 1186, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1335, in execute_sql
    cursor.execute(sql, params)
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/USERNAME/PROJECT_PATH/env/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: UNIQUE constraint failed: auth_user.username

1 个答案:

答案 0 :(得分:0)

非常抱歉,社区无法解决此问题,因为我的代码示例未显示实际的错误行。我更改了问题中提供的代码行,并在下面的代码中添加了名字和姓氏,并且认为这不会有所作为,因为脚本之前已经正确运行了。

这是导致错误的行:

dude, created = User.objects.get_or_create(username='testguy', first_name='Testguy', last_name='Testguy')

引发异常是因为数据库中已经存在一个用户名“ testguy”的用户,但是他没有相同的数据。因此,“ get_or_create”既无法获得用户也无法创建用户。

我的解决方案是删除数据库中的用户并重新运行脚本。