[Fixed]-How to enforce different values in multiple ForeignKey fields for Django

7👍

This cannot be done through pure Django. There’s a ticket for adding CHECK constraints: https://code.djangoproject.com/ticket/11964

To ensure the situation where team_home == team_visitors never happens, you will need to add a custom constraint to the table which is database dependent. As an example, in MySQL and PostgresQL:

alter table myapp_match add constraint match_teams_not_equal check (team_home_id <> team_visitors_id);

This will cause the database to raise an integrity error when a save happens. Note that there are also databases where you cannot implement such a constraint.

You can partially ensure that team_home != team_visitors by overriding the save method:

class Match(models.Model):
    ....
    def save(self, *args, **kwargs):
        if self.team_home == self.team_visitors:
            raise Exception('attempted to create a match object where team_home == team_visitors')
        super(Match, self).save(*args, **kwargs)

However, someone can modify the database directly, or use update queryset in Django and you can still end up with Match objects where team_home == team_visitor.

19👍

You can use this CheckConstraint in class Meta of your django model:

class Meta:
    constraints = [
        models.CheckConstraint(
            check=~Q(team_home=F('team_visitors')),
            name='team_home_and_team_visitors_can_not_be_equal')
    ]

-4👍

I believe you can use the unique_together property of the Meta class: unique_together

class Match(models.Model):
    ...
    class Meta:
        unique_together = ("team_home", "team_visitors")

Leave a comment