2

I am trying to create a lot of users for testing my Django application. I've used this method on other models and it works, but here it doesn't. When executed it states the error below. The latest Django version (2.2.6) was used. The documentation states:

(...) setting the ignore_conflicts parameter to True tells the database to ignore failure to insert any rows that fail constraints such as duplicate unique values.

My Code:

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)

Full error traceback:

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
creyD
  • 1,972
  • 3
  • 26
  • 55
  • It might be an issue with `auth_user` - sounds like youre using a security package and while the db might allow ignoring of constraints the security package might not? – Craicerjack Oct 15 '19 at 15:48
  • I don't use a security package, as far as I know... However I use one-to-one fields on separate classes, for managing other information about the users, could that be the problem? They won't be created as their hooks aren't triggered by `bulk_create`. – creyD Oct 15 '19 at 15:54
  • I dont know - but looking at the error it seems to be specifically related to a constraint around auth_user. – Craicerjack Oct 15 '19 at 16:00
  • Then I really don't know where I went wrong... – creyD Oct 15 '19 at 16:13
  • What database are you using? This feature [doesn't work](https://stackoverflow.com/a/54189488/10746224) for PostgreSQL < 9.5 and Oracle. – Lord Elrond Oct 15 '19 at 18:02
  • @CalebGoodman I am using a local sqlite3 database. – creyD Oct 15 '19 at 18:34
  • I'm thinking that this error is being thrown *before* your query hits the db, which means that Craicerjack is probably right. Can you provide the full traceback? – Lord Elrond Oct 15 '19 at 18:45
  • @CalebGoodman I added it to the question, however it looks pretty messy, should I put it on pastebin or something? – creyD Oct 15 '19 at 18:56
  • I changed it to be indented code instead of the highlighting, but I think most users prefer having it in the question, instead of in an external link. – Lord Elrond Oct 15 '19 at 18:59

1 Answers1

0

I am very sorry, this question couldn't be solved by the community, because my code sample doesn't show the actual error line. I changed the lines provided in the question and added first and last name to the code below and didn't think it would make a difference, as the script already ran properly before.

This was the line causing the error:

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

The exception was thrown because in the database a user with the username "testguy" was already there, but he didn't have the same data. So 'get_or_create' could neither get the user nor create it.

My solution was to delete the user in the database and to rerun the script.

creyD
  • 1,972
  • 3
  • 26
  • 55