Skip to content Skip to sidebar Skip to footer

After Extending User Profile To Include A Profile Model, Django Throws Unique Constraint Failed Error

I extended the User model to include a profile with the following code: class Profile(models.Model): PTO_TIER_CHOICES = ( (200.0, 'Boss 5-10 Years'), (160.0, 'B

Solution 1:

I changed the LeaveHistory model from:

user = models.OneToOneField(User, on_delete=models.CASCADE) 

to:

user = models.ForeignKey(User, on_delete=models.CASCADE) 

but I wasn't applying the migrations correctly which resulted in my UNIQUE Constraint Error. I kept running

python manage.py makemigrations

which didn't detect any new changes in my accounts app. The correct way that worked for me was to specify the app name in the makemigrations command,

python manage.py makemigrations [app name],

which detected the changes and I could then run python manage.py migrate and it succesfully applied the changes to my database.

Solution 2:

That is because you are linking LeaveHistory to the User model via foreign key. Instead, you should be linking to the Profile model.

classLeaveHistory(models.Model):
   ...
   profile = models.ForeignKey(Profile, on_delete=models.CASCADE)

Post a Comment for "After Extending User Profile To Include A Profile Model, Django Throws Unique Constraint Failed Error"