'Django throws an integrity error while deleting an user
I have a project in which students can be added, when i try to delete the student who has been added django shows an error like this
IntegrityError at /student/delete/1
FOREIGN KEY constraint failed
Request Method: GET
Request URL: http://127.0.0.1:8000/student/delete/1
Django Version: 3.1.1
Exception Type: IntegrityError
Exception Value:
FOREIGN KEY constraint failed
Exception Location: C:\Users\name\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\base\base.py, line 242, in _commit
Python Executable: C:\Users\name\AppData\Local\Programs\Python\Python310\python.exe
Python Version: 3.10.2
Python Path:
['C:\\Users\\name\\Desktop\\test-projects\\college-management-sytem_withstaffsentmailFinal',
'C:\\Users\\name\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip',
'C:\\Users\\name\\AppData\\Local\\Programs\\Python\\Python310\\DLLs',
'C:\\Users\\name\\AppData\\Local\\Programs\\Python\\Python310\\lib',
'C:\\Users\\name\\AppData\\Local\\Programs\\Python\\Python310',
'C:\\Users\\name\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages']
Server time: Mon, 18 Apr 2022 06:35:06 +0100
here is the django model
class Student(models.Model):
admin = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
course = models.ForeignKey(Course, on_delete=models.DO_NOTHING, null=True, blank=False)
session = models.ForeignKey(Session, on_delete=models.DO_NOTHING, null=True)
def __str__(self):
return self.admin.last_name + ", " + self.admin.first_name
class Staff(models.Model):
course = models.ForeignKey(Course, on_delete=models.DO_NOTHING, null=True, blank=False)
admin = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
def __str__(self):
return self.admin.last_name + " " + self.admin.first_name
is there any way to fix this error ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
