# 删除保护错误：无法删除 'Author' 模型的某些实例，因为它们通过受保护的外键 'Book.author' 被引用

- **ID:** `python/django-foreign-key-delete-protection`
- **领域:** python
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

删除具有 on_delete=PROTECT 子对象的父对象。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   Delete child objects first: Book.objects.filter(author=author).delete() then author.delete()
   ```
2. **** (80% 成功率)
   ```
   Change on_delete=models.CASCADE in model and run migrations to allow cascading delete.
   ```

## 无效尝试

- **** — Changing on_delete to CASCADE in model requires migration and may delete data unintentionally. (60% 失败率)
- **** — Using raw SQL DELETE ignores Django's protection and can cause orphaned records. (50% 失败率)
