title: Django unique together constraint in two directions
body: <p>So I have some models that look like this</p>
  <pre><code>class Person(BaseModel):
      name = models.CharField(max_length=50)
      # other fields declared here...
      friends = models.ManyToManyField(
          to=&quot;self&quot;,
          through=&quot;Friendship&quot;,
          related_name=&quot;friends_to&quot;,
          symmetrical=True
      )
  
  class Friendship(BaseModel):
      friend_from = models.ForeignKey(
          Person, on_delete=models.CASCADE, related_name=&quot;friendships_from&quot;)
      friend_to = models.ForeignKey(
          Person, on_delete=models.CASCADE, related_name=&quot;friendships_to&quot;)
      state = models.CharField(
          max_length=20, choices=FriendshipState.choices, default=FriendshipState.pending)
  </code></pre>
  <p>So basically, I'm trying to model a Facebook-like friends situation, where there are different persons and one can ask any other person to be friends. That relationship is expressed through this last model <code>Friendship</code>.</p>
  <p>So far so good. <strong>But there are three situations I'd like to avoid:</strong></p>
  <ul>
  <li>
  <ol>
  <li>A Friendship can't have the same person in the <code>friend_from</code> and <code>friend_to</code> fields</li>
  </ol>
  </li>
  <li>
  <ol start="2">
  <li>Only one <code>Friendship</code> for a set of two Friends should be allowed.</li>
  </ol>
  </li>
  </ul>
  <p>The closest I've got to that, is adding this under the <code>Friendship</code> model:</p>
  <pre><code>class Meta:
      constraints = [
          constraints.UniqueConstraint(
              fields=['friend_from', 'friend_to'], name=&quot;unique_friendship_reverse&quot;
          ),
          models.CheckConstraint(
              name=&quot;prevent_self_follow&quot;,
              check=~models.Q(friend_from=models.F(&quot;friend_to&quot;)),
          )
      ]
  </code></pre>
  <p>This totally solves situation 1, avoiding someone to befriend with himself, using the <code>CheckConstraint</code>.
  And partially solves situation number 2, because it avoids having two <code>Friendships</code> like this:</p>
  <pre><code>p1 = Person.objects.create(name=&quot;Foo&quot;)
  p2 = Person.objects.create(name=&quot;Bar&quot;)
  Friendship.objects.create(friend_from=p1, friend_to=p2) # This one gets created OK
  Friendship.objects.create(friend_from=p1, friend_to=p2) # This one fails and raises an IntegrityError, which is perfect 
  </code></pre>
  <p>Now there's one case that'd like to avoid that still can happen:</p>
  <pre><code> Friendship.objects.create(friend_from=p1, friend_to=p2) # This one gets created OK
   Friendship.objects.create(friend_from=p2, friend_to=p1) # This one won't fail, but I'd want to
  </code></pre>
  <p><strong>How would I make the <code>UniqueConstraint</code> work in this &quot;two directions&quot;? Or how could I add another constraint to cover this case?</strong></p>
  <p>Of course, I could overwrite the <code>save</code> method for the model or enforce this in some other way, but I'm curious about how this should be done at the database level.</p>