#34523: update_or_create not work in parallel insertion
-------------------------------------+-------------------------------------
     Reporter:  gatello-s            |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  4.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  update_or_create     |             Triage Stage:
  TransactionManagementError         |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by David Sanders):

 I ran the test above (I had to add managed=False to the model) on 3.2.16
 and I get the same error as it does on main.

 Here's the failure with stack trace:

 {{{
 ======================================================================
 ERROR: test_update_or_create
 (ticket_34523.tests.TransactionManagementErrorTest)
 ----------------------------------------------------------------------
 Traceback (most recent call last):
   File "/Users/dsanders/projects/django-sample/django/db/models/query.py",
 line 581, in get_or_create
     return self.get(**kwargs), False
   File "/Users/dsanders/projects/django-sample/django/db/models/query.py",
 line 435, in get
     raise self.model.DoesNotExist(
 ticket_34523.tests.TransactionManagementErrorTest.TestModel.DoesNotExist:
 TestModel matching query does not exist.

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "/Users/dsanders/projects/django-
 sample/django/db/backends/utils.py", line 84, in _execute
     return self.cursor.execute(sql, params)
   File "/Users/dsanders/projects/django-
 sample/django/db/backends/mysql/base.py", line 73, in execute
     return self.cursor.execute(query, args)
   File "/Users/dsanders/projects/django-
 sample/.direnv/python-3.10.8/lib/python3.10/site-
 packages/MySQLdb/cursors.py", line 206, in execute
     res = self._query(query)
   File "/Users/dsanders/projects/django-
 sample/.direnv/python-3.10.8/lib/python3.10/site-
 packages/MySQLdb/cursors.py", line 319, in _query
     db.query(q)
   File "/Users/dsanders/projects/django-
 sample/.direnv/python-3.10.8/lib/python3.10/site-
 packages/MySQLdb/connections.py", line 255, in query
     _mysql.connection.query(self, query)
 MySQLdb.IntegrityError: (1062, "Duplicate entry '1' for key
 'test_model_update_or_create.PRIMARY'")

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

 Traceback (most recent call last):
   File "/Users/dsanders/projects/django-sample/ticket_34523/tests.py",
 line 44, in test_update_or_create
     self.TestModel.objects.update_or_create(id=1, defaults={"field": 2})
   File "/Users/dsanders/projects/django-
 sample/django/db/models/manager.py", line 85, in manager_method
     return getattr(self.get_queryset(), name)(*args, **kwargs)
   File "/Users/dsanders/projects/django-sample/django/db/models/query.py",
 line 608, in update_or_create
     obj, created = self.select_for_update().get_or_create(defaults,
 **kwargs)
   File "/Users/dsanders/projects/django-sample/django/db/models/query.py",
 line 588, in get_or_create
     return self.create(**params), True
   File "/Users/dsanders/projects/django-sample/ticket_34523/tests.py",
 line 16, in create
     return super().create(**kwargs)
   File "/Users/dsanders/projects/django-sample/django/db/models/query.py",
 line 453, in create
     obj.save(force_insert=True, using=self.db)
   File "/Users/dsanders/projects/django-sample/django/db/models/base.py",
 line 739, in save
     self.save_base(using=using, force_insert=force_insert,
   File "/Users/dsanders/projects/django-sample/django/db/models/base.py",
 line 776, in save_base
     updated = self._save_table(
   File "/Users/dsanders/projects/django-sample/django/db/models/base.py",
 line 881, in _save_table
     results = self._do_insert(cls._base_manager, using, fields,
 returning_fields, raw)
   File "/Users/dsanders/projects/django-sample/django/db/models/base.py",
 line 919, in _do_insert
     return manager._insert(
   File "/Users/dsanders/projects/django-
 sample/django/db/models/manager.py", line 85, in manager_method
     return getattr(self.get_queryset(), name)(*args, **kwargs)
   File "/Users/dsanders/projects/django-sample/django/db/models/query.py",
 line 1270, in _insert
     return query.get_compiler(using=using).execute_sql(returning_fields)
   File "/Users/dsanders/projects/django-
 sample/django/db/models/sql/compiler.py", line 1416, in execute_sql
     cursor.execute(sql, params)
   File "/Users/dsanders/projects/django-
 sample/django/db/backends/utils.py", line 66, in execute
     return self._execute_with_wrappers(sql, params, many=False,
 executor=self._execute)
   File "/Users/dsanders/projects/django-
 sample/django/db/backends/utils.py", line 75, in _execute_with_wrappers
     return executor(sql, params, many, context)
   File "/Users/dsanders/projects/django-
 sample/django/db/backends/utils.py", line 79, in _execute
     with self.db.wrap_database_errors:
   File "/Users/dsanders/projects/django-sample/django/db/utils.py", line
 90, in __exit__
     raise dj_exc_value.with_traceback(traceback) from exc_value
   File "/Users/dsanders/projects/django-
 sample/django/db/backends/utils.py", line 84, in _execute
     return self.cursor.execute(sql, params)
   File "/Users/dsanders/projects/django-
 sample/django/db/backends/mysql/base.py", line 73, in execute
     return self.cursor.execute(query, args)
   File "/Users/dsanders/projects/django-
 sample/.direnv/python-3.10.8/lib/python3.10/site-
 packages/MySQLdb/cursors.py", line 206, in execute
     res = self._query(query)
   File "/Users/dsanders/projects/django-
 sample/.direnv/python-3.10.8/lib/python3.10/site-
 packages/MySQLdb/cursors.py", line 319, in _query
     db.query(q)
   File "/Users/dsanders/projects/django-
 sample/.direnv/python-3.10.8/lib/python3.10/site-
 packages/MySQLdb/connections.py", line 255, in query
     _mysql.connection.query(self, query)
 django.db.utils.IntegrityError: (1062, "Duplicate entry '1' for key
 'test_model_update_or_create.PRIMARY'")
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34523#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070187c2bbcbdd-e4e3b78c-beb2-43b8-9a1a-1a8804f08ef5-000000%40eu-central-1.amazonses.com.

Reply via email to