[Answer]-Problems with column user_id is not unique

1👍

You need to change your Question model, as @DanielRoseman says you should change the OneToOneField you’re using:

class Question(models.Model):
    # ... ... .. other fields .... ....
    user = models.OneToOneField(User, unique=True)

You need to change from OneToOneField to ForeignKey in case that one question can be related only with one user but an user can be related with more than one question (Many to one relation):

class Question(models.Model):
    # ... ... .. other fields .... ....    
    user = models.ForeignKey(User)

or probably ManyToManyField is the most apropiated here, in case that many answers can be related with many users:

class Question(models.Model):
    # ... ... .. other fields .... ....
    user = models.ManyToMany(User)

Leave a comment