# Django数据库完整性错误：唯一约束失败：auth_user.username

- **ID:** `python/django-user-creation-race`
- **领域:** python
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   Use transaction.atomic() and select_for_update to lock the row before checking existence.
   ```

## 无效尝试

- **** — User isn't created, but no feedback given; data loss. (60% 失败率)
- **** — Still can raise IntegrityError under concurrency. (80% 失败率)
