У меня есть эта модель системы. Проект содержит записи. Но когда я пытаюсь удалить Project, я получаю эту ошибку. Я получаю IntegrityError (сбой ограничения FOREIGN KEY) при удалении проекта. Как я могу решить эту ошибку?
Вот мои отношения с внешним ключом от models.py
class Project(models.Model):
project_author = models.ForeignKey(User, on_delete=models.CASCADE)
project_entry = models.ForeignKey(Project, on_delete=models.CASCADE)
class Entry(models.Model):
entry_author = models.ForeignKey(User, on_delete=models.CASCADE)
class Comment(models.Model):
post = models.ForeignKey(Project, blank=True, null=True, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
reply = models.ForeignKey('Comment', null=True, blank=True,related_name="replies", on_delete=models.CASCADE)
class EntryComment(models.Model):
post = models.ForeignKey(Entry, blank=True, null=True, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
entryreply = models.ForeignKey('EntryComment', null=True, blank=True,related_name="entryreplies", on_delete=models.CASCADE)
Traceback
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/lift/delete/
Traceback (most recent call last):
File "D:\Projects\django_project\django_venv\lib\site-packages\django\db\backends\base\base.py", line 243, in _commit
return self.connection.commit()
The above exception (FOREIGN KEY constraint failed) was the direct cause of the following exception:
File "D:\Projects\django_project\django_venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "D:\Projects\django_project\django_venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "D:\Projects\django_project\django_venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Projects\django_project\django_venv\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "D:\Projects\django_project\django_venv\lib\site-packages\django\contrib\auth\mixins.py", line 52, in dispatch
return super().dispatch(request, *args, **kwargs)
File "D:\Projects\django_project\django_venv\lib\site-packages\django\contrib\auth\mixins.py", line 109, in dispatch
return super().dispatch(request, *args, **kwargs)
File "D:\Projects\django_project\django_venv\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "D:\Projects\django_project\django_venv\lib\site-packages\django\views\generic\edit.py", line 218, in post
return self.delete(request, *args, **kwargs)
File "D:\Projects\django_project\django_venv\lib\site-packages\django\views\generic\edit.py", line 213, in delete
self.object.delete()
File "D:\Projects\django_project\django_venv\lib\site-packages\django\db\models\base.py", line 938, in delete
return collector.delete()
File "D:\Projects\django_project\django_venv\lib\site-packages\django\db\models\deletion.py", line 338, in delete
sender=model, instance=obj, using=self.using
File "D:\Projects\django_project\django_venv\lib\site-packages\django\db\transaction.py", line 232, in __exit__
connection.commit()
File "D:\Projects\django_project\django_venv\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "D:\Projects\django_project\django_venv\lib\site-packages\django\db\backends\base\base.py", line 267, in commit
self._commit()
File "D:\Projects\django_project\django_venv\lib\site-packages\django\db\backends\base\base.py", line 243, in _commit
return self.connection.commit()
File "D:\Projects\django_project\django_venv\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "D:\Projects\django_project\django_venv\lib\site-packages\django\db\backends\base\base.py", line 243, in _commit
return self.connection.commit()
Exception Type: IntegrityError at /lift/delete/
Exception Value: FOREIGN KEY constraint failed
Удалить вид: