Islam Mansour
Islam Mansour

Reputation: 79

unique constrain fails with foreignKey field

I want to make a position model with a foreign key to a category model, but i get a unique constrain error when adding 2 position to one category although the category field is foreignkey model not one-to-one field

i tried many things but it didn't work

class Category(models.Model):
    name = models.CharField(max_length=50, unique=True)
    _type = models.CharField(max_length=20, null=True)

class Position(models.Model):
    name = models.CharField(max_length=50, unique=True)
    category = models.IntegerField(Category, on_delete=models.CASCADE)

Upvotes: 0

Views: 28

Answers (1)

Yousuf Jawwad
Yousuf Jawwad

Reputation: 3097

please take a look at your Position model. The category field should be defined as either

category = models.ForeignKey(Category, on_delete=models.CASCADE)

or

category = models.OneToOneField(Category, on_delete=models.CASCADE)

also, it is generally not accepted that you define a field starting with _ in django.

Upvotes: 1

Related Questions