unique_together中的多个元组 [英] Multiple tuples in unique_together

查看:104
本文介绍了unique_together中的多个元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我定义一个模型并在Meta中使用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.

推荐答案

每个元组都会在 CREATE TABLE 查询中添加一个离散的 UNIQUE 子句。这样,每个元组都是独立的,并且如果违反任何数据完整性约束,则插入将失败。

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.

这篇关于unique_together中的多个元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆