I was getting this error when running some tests on my APP .
python3 manage.py test tasks
The error :
django.db.utils.IntegrityError: The row in table ‘%’ with primary key '1' has an invalid foreign key: %.user_id contains a value '1' that does not have a corresponding value in auth_user.id.
The offending code :
django models.ForeignKey(User, on_delete=models.CASCADE, default=1)
Here's what I searched when looking for an answer:
Django how to add a default user id to model
Django model foreignKey user pass in default user
Django does not have a corresponding value in auth_user.id
I tried a few different methods to resolve including removing all my migration file and running the migrate commands again .
python3 manage.py makemigrations
Python3 manage.py migrate
What I found worked though was to change my code to the following line ( and then run the above migration commands again )
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)
No comments:
Post a Comment