- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我查看包含 unique_together 语句的 models.py 的 sqlall 时,我没有注意到任何看起来像执行的东西。
在我看来,我可以想象这些知识可能会帮助数据库优化查询,如下所示:
“我已经找到包含垃圾邮件 42 和鸡蛋 91 的行,因此在搜索鸡蛋 91 时,我不再需要检查包含垃圾邮件 42 的行。”
我认为这些知识对数据库有帮助吗?
它不是以这种方式强制执行的(即,它仅由 ORM 强制执行),我说得对吗?
如果两者都是,这是一个缺陷吗?
最佳答案
这是一个示例。假设您有模型:
class UserConnectionRequest(models.Model):
sender = models.ForeignKey(UserProfile, related_name='sent_requests')
recipient = models.ForeignKey(UserProfile, related_name='received_requests')
connection_type = models.PositiveIntegerField(verbose_name=_(u'Connection type'), \
choices=UserConnectionType.choices())
class Meta:
unique_together = (("sender", "recipient", "connection_type"),)
运行 sqlall 它返回:
CREATE TABLE "users_userconnectionrequest" (
"id" serial NOT NULL PRIMARY KEY,
"sender_id" integer NOT NULL REFERENCES "users_userprofile" ("id") DEFERRABLE INITIALLY DEFERRED,
"recipient_id" integer NOT NULL REFERENCES "users_userprofile" ("id") DEFERRABLE INITIALLY DEFERRED,
"connection_type" integer,
UNIQUE ("sender_id", "recipient_id", "connection_type")
)
当此模型在数据库上正确同步时,它具有唯一约束(postgres):
CONSTRAINT users_userconnectionrequest_sender_id_2eec26867fa22bfa_uniq UNIQUE (sender_id, recipient_id, connection_type),
关于postgres : enforced by ORM or DB? 上的 Django unique_together,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6643732/
我想用呈现 GenericForeignKey 字段的模型来概括我的工作流程。 所以我创建了父类 GFKModel: class GFKModel(models.Model): target_
在此: class Administrator(models.Model): user = models.OneToOneField(User, primary_key=True) a
我有两个模型:Common 和 ARecord。 ARecord 与 Common 有外键关系。我想通过 ARecord 和 Common 的项目组合来确保 ARecord 是独一无二的。 class
当尝试在两个外键约束上使用 unique_together 时,出现以下错误: CommandError: System check identified some issues: ERRORS: m
目前我有三个模型: class Tutorial(models.Model): title = models.CharField(max_length=100) description
我正在致力于通过 Django Rest Framework 在移动应用 API 中实现 Like/Unlike 功能。 所以我有以下模型: class PlaylistLikes(models.Mo
当我定义一个模型并在 Meta 中使用 unique_together 时,我可以定义多个元组。这些是要进行 OR 运算还是 AND 运算?也就是说,我有一个模型,其中 class MyModel(m
我的代码中两个字段条目唯一性检查存在问题。 我使用 unique_together 定义了一个模型来检查每个用户字段记录的唯一性,但它接受该用户添加的重复条目。 模型.py from django.d
有没有办法过滤 django 模型,其中两个字段在一起是唯一的? 例如,我有这个模型: class Sequence(models.Model): id = models.Char
我无法让 Django (1.5) 在 3 列上创建 MySQL UNIQUE 索引,尽管我已经遵循了在 SO 上找到的所有建议。我的模型如下所示: class Loc(models.Model):
我有以下内容: class AccountAdmin(models.Model): account = models.ForeignKey(Account) is_master = m
我需要检查 unique_together 只有在两个字段都提供给管理员创建 Form 或通过 API 请求两个 create 和 update 调用。如果发生这种情况并且字段不是 unique_to
我有一个带有 unique_together 的模型定义为 3 个字段一起唯一: class MyModel(models.Model): clid = models.AutoField(pr
我显然不明白如何正确地做到这一点,有人可以让我直截了当。这是模型: class Team(models.Model): teamID=models.CharField(max_length=25
在 Django 中使用 GenericForeign Relations 时,我不确定是我做错了还是在处理唯一约束时出现了一些问题。 当我尝试保存对象时(例如在 Admin 中),我从数据库中收到唯
我看到一些人在我之前遇到过这个问题,但是在旧版本的 Django 上,我在 1.2.1 上运行。 我有一个看起来像的模型: class Category(models.Model): objects
这是我尝试使用广义自然键模型管理器的尝试。类似于the docs,不同之处在于它尝试(未成功)从Meta.unique_together属性确定自然键字段名称。 class NaturalKeyMod
unique_together 不起作用,它只在第一个字段上设置唯一约束并忽略第二个字段。有什么办法可以强制执行唯一约束吗? class BaseModel(models.Model): id
我有以下模型: class LocationPoint(models.Model): latitude = models.DecimalField(max_digits=9, decimal_
我在字段之间使用了unique_together,经过一段时间和新的要求,我决定要删除它,因此我从模型定义中删除了它,但似乎仍然会引起“重复输入”问题。 我是否必须在数据库上放一些约束?在这种情况下,
我是一名优秀的程序员,十分优秀!