# RuntimeError: 不允许访问数据库，请使用 "django_db" 标记或 "db" 或 "transactional_db" fixtures 来启用它。

- **ID:** `python/pytest-django-fixture-db-access`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

pytest-django 默认阻止数据库访问。测试尝试查询数据库但没有使用 django_db 标记或 db fixture。

## 版本兼容性

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

## 解决方案

1. **** (98% 成功率)
   ```
   @pytest.mark.django_db
def test_create_user():
    User.objects.create(username='test')
   ```
2. **** (95% 成功率)
   ```
   def test_create_user(db):
    User.objects.create(username='test')
   ```
3. **** (90% 成功率)
   ```
   @pytest.mark.django_db(transaction=True)
def test_transaction():
    ...
   ```

## 无效尝试

- **** — The block is enforced by pytest-django, not Django settings. (90% 失败率)
- **** — Breaks Django test integration and may cause other failures. (95% 失败率)
- **** — pytest-django still applies the block unless marked. (80% 失败率)
