Within transaction.atomic()
I am deleting and re-creating objects.
Django sits behind a 3-worker gunicorn, database is postgres.
Issue: When hitting the server with a lot of requests simultaneously, Django is throwing a few IntegrityErrors:
IntegrityError: duplicate key value violates unique constraint "atomic_atomictest_name_key"
DETAIL: Key (name)=(foo) already exists.
Example:
models.py
class AtomicTest(models.Model):
name = models.TextField(null=False, unique=True)
views.py
def test_atomic(request):
with transaction.atomic():
models.AtomicTest.objects.filter(name='foo').delete()
models.AtomicTest(name='foo').save()
return http.HttpResponse('OK')
The IntegrityErrors shouldn't occur in my understanding. Can anyone explain why?