Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple tuples in unique_together

When I am defining a model and using unique_together in the Meta, I can define more than one tuple. Are these going to be ORed or ANDed? That is lets say I have a model where

class MyModel(models.Model):
    druggie = ForeignKey('druggie', null=True)
    drunk = ForeignKey('drunk', null=True)
    quarts = IntegerField(null=True)
    ounces = IntegerField(null=True)

    class Meta:
        unique_together = (('drunk', 'quarts'),
                           ('druggie', 'ounces'))

either both druggie and ounces are unique or both drunk and quarts are unique, but not both.

like image 575
Mad Wombat Avatar asked Jan 26 '16 21:01

Mad Wombat


1 Answers

Each tuple results in a discrete UNIQUE clause being added to the CREATE TABLE query. As such, each tuple is independent and an insert will fail if any data integrity constraint is violated.

like image 73
Ignacio Vazquez-Abrams Avatar answered Oct 26 '22 05:10

Ignacio Vazquez-Abrams