Django IntegrityError Duplicate entry on Foreignkey
===

I am having a subtle error with a duplicate key integrity error on django with a set of foreignkey relations.

I have the following function: 


    def update_relationship(actor, action, status, target):
      existing = Relation.objects.filter(actor=actor, target=target)

      # If this relation is 'on', turn it off
      if Relation.objects.filter(actor=actor, target=target, status=status):
        Relation.objects.filter(actor=actor, target=target).update(status="")
      # If this relationship is not on, turn it on
      else:
        created = True
        if existing:
          existing.update(status=status)
        else:
          Relation.objects.create(actor=actor, target=target, status=status)

As you can see, I am testing to see if the relationship exists already in the database and then updating it if it does exist and creating a new row if it does not.  However, it seems under some conditions that I can't reproduce, Django is giving me a duplicate key error, even for conditions where, as far as I can tell, there is only one instance of that.

For reference, here is the the model definition:

    class Relation(models.Model):
      Status = Choices(('L', 'Like', 'Like'), 
                       ('D', 'Dislike', 'Dislike'), 
                       ('S', 'Save', 'Save'))
      actor     = models.ForeignKey('members.Member', related_name='relations')
      target    = models.ForeignKey('members.Member', related_name='reverse_relations')
      status    = models.CharField(choices=Status, max_length=10)
      created   = models.DateTimeField('created', auto_now_add=True)

      notified        = models.BooleanField(default=False)
      notified_mutual = models.BooleanField(default=False)
    
      class Meta:
        unique_together = (('actor', 'target'),)
        ordering = ('created',)
        verbose_name = 'Relation'
        verbose_name_plural = 'Relations'