python data_error ai_generated true

django.db.models.deletion.ProtectedError: Cannot delete some instances of model 'Author' because they are referenced through protected foreign keys: 'Article.author'

ID: python/django-orm-delete-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-10-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

尝试删除被外键引用的对象,且外键设置了PROTECT

generic

中文

删除操作被外键保护约束阻止,关联对象存在

Workarounds

  1. 95% success
    article.delete() 然后 author.delete()
  2. 90% success
    在模型中设置on_delete=models.CASCADE

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Django ORM没有force_delete方法,会破坏数据完整性

  2. 80% fail

    删除操作无法完成