python data_error ai_generated true

django.db.utils.IntegrityError: UNIQUE constraint failed: auth_user.username

ID: python/django-user-creation-race

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-11-03First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

Two concurrent requests attempt to create a user with the same username.

generic

中文

两个并发请求尝试创建具有相同用户名的用户。

Workarounds

  1. 90% success
    Use transaction.atomic() and select_for_update to lock the row before checking existence.

Dead Ends

Common approaches that don't work:

  1. 60% fail

    User isn't created, but no feedback given; data loss.

  2. 80% fail

    Still can raise IntegrityError under concurrency.