Hi there is this problem I have can anyone solve this ?
here is my django model
JavaScript
x
18
18
1
class Question(models.Model):
2
user = models.ForeignKey(User,on_delete=models.SET_NULL,null=True)
3
title = models.CharField(max_length=255,null=True,blank=False)
4
content = models.TextField(null=True,blank=False)
5
subject = models.ForeignKey(Subject,on_delete=models.SET_NULL,null=True,related_name="subject_question")
6
topicTag = models.ManyToManyField(TopicTag, related_name='questionTopic', blank=True)
7
image = models.ImageField(blank=True, null=True)
8
createdAt = models.DateTimeField(auto_now_add=True)
9
votes = models.ManyToManyField(User, related_name='questionUser', blank=True, through='QuestionVote')
10
answer_count = models.IntegerField(default=0,null=True,blank=True)
11
difficulty = models.ForeignKey(Difficulty,on_delete=models.SET_NULL,null=True,related_name="difficulty")
12
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
13
14
def __str__(self):
15
return self.title
16
17
18
and here is the error code
JavaScript
1
2
1
django.db.utils.OperationalError: foreign key mismatch - "question_question" referencing "question_subject"
2
and here is the subject model
JavaScript
1
6
1
class Subject(models.Model):
2
name = models.CharField(max_length=255,primary_key=True,null=False,blank=False)
3
4
def __str__(self):
5
return self.name
6
Advertisement
Answer
This error is likely do to a migration issue. If you are in early development, the quickest solution is to delete migration files and SQLite db, re-run python manage.py makemigrations
and python manage.py migrate
. Alternatively you can revert all migrations with python manage.py name_of_app migrate -- zero
.
See here for more information on reverting migrations.