0

So let's say I have the following Django models defined:

class Model(models.Model):
    model_id = models.IntegerField(default=0, unique=true)
    content = models.TextField()

class ModelRelation(models.Model):
    relation_id = models.IntegerField(default=0, unique=true)
    model_id1 = models.ForeignKey(Model, on_delete=models.CASCADE)
    model_id2 = models.ForeignKey(Model, on_delete=models.CASCADE)

With a regular UniqueConstraint in the constraints option of the Meta class,
a foreign key pair of model_id1 = 1 and model_id2 = 2 is allowed to coexist with
a foreign key pair of model_id1 = 2 and model_id2 = 1.

How do I implement a behaviour like in the first answer to Postgresql enforce unique two-way combination of columns in Django? How do you even make an unique index in Django?

1 Answers1

0

You could use a ManyToMany Field which is automatically symmetrical: ManyToManyField Django Docs

buguser
  • 271
  • 3
  • 4