A field on a model, foo = models.ForeignKey(Foo)
will automatically add a database index for the column, in order to make look-ups faster. That's good and well, but Django's docs don't state whether the fields in a model-meta's unique_together
receive the same treatment. I happen to have a model in which one char field which is listed in unique_together
requires an index for quick lookups. I know that it won't hurt anything to add a duplicate db_index=True
in the field definition, but I'm curious.
模型上的字段foo = models.ForeignKey(Foo)将自动为列添加数据库索引,以便更快地查找。这很好,但Django的文档没有说明模型元的unique_together中的字段是否接受相同的处理。我碰巧有一个模型,其中unique_together中列出的一个char字段需要一个快速查找索引。我知道在字段定义中添加重复的db_index = True不会有任何损害,但我很好奇。
4 个解决方案
#1
9
If unique_together
does add an index, it will be a multiple column index.
如果unique_together确实添加了索引,那么它将是一个多列索引。
If you want one of the columns to be indexed individually, I believe you need to specify db_index=True
in the field definition.
如果您希望单独索引其中一列,我相信您需要在字段定义中指定db_index = True。
#2
11
For anyone coming here wondering if they need an index_together
in addition to unique_together
to get the index's performance benefit, the answer for Postgres is no, they are functionally the same.
对于任何来到这里的人,想知道除了unique_together还需要index_together以获得索引的性能优势,Postgres的答案是否定的,它们在功能上是相同的。
#3
4
In Django 1.5 and higher, you can use the {Model}.Meta.index_together
class attribute. If you had two fields named foo
and bar
, you would add:
在Django 1.5及更高版本中,您可以使用{Model} .Meta.index_together类属性。如果你有两个名为foo和bar的字段,你会添加:
class Meta(object):
index_together = unique_together = [
['foo', 'bar']
]
If you have only one set of unique fields, you can use a one-dimensional iterable for unique_together
. However, the documentation does not indicate that the same applies to index_together
.
如果您只有一组唯一字段,则可以对unique_together使用一维可迭代。但是,文档并未指出同样适用于index_together。
This would also be okay:
这也没关系:
class Meta(object):
unique_together = 'foo', 'bar'
index_together = [
['foo', 'bar']
]
This, however, is NOT supported by the documentation:
但是,文档不支持此操作:
class Meta(object):
unique_together = 'foo', 'bar'
index_together = 'foo', 'bar'
#4
1
According to the docs, it will only enforce uniqueness on database level. I think generally making a field unique does not imply it has an index. Though you could also simply check on db level if the index exists. Everything indicates though it does not.
根据文档,它只会强制数据库级别的唯一性。我认为通常使字段唯一并不意味着它有一个索引。虽然如果索引存在,您也可以简单地检查数据库级别。一切都表明虽然没有。
#1
9
If unique_together
does add an index, it will be a multiple column index.
如果unique_together确实添加了索引,那么它将是一个多列索引。
If you want one of the columns to be indexed individually, I believe you need to specify db_index=True
in the field definition.
如果您希望单独索引其中一列,我相信您需要在字段定义中指定db_index = True。
#2
11
For anyone coming here wondering if they need an index_together
in addition to unique_together
to get the index's performance benefit, the answer for Postgres is no, they are functionally the same.
对于任何来到这里的人,想知道除了unique_together还需要index_together以获得索引的性能优势,Postgres的答案是否定的,它们在功能上是相同的。
#3
4
In Django 1.5 and higher, you can use the {Model}.Meta.index_together
class attribute. If you had two fields named foo
and bar
, you would add:
在Django 1.5及更高版本中,您可以使用{Model} .Meta.index_together类属性。如果你有两个名为foo和bar的字段,你会添加:
class Meta(object):
index_together = unique_together = [
['foo', 'bar']
]
If you have only one set of unique fields, you can use a one-dimensional iterable for unique_together
. However, the documentation does not indicate that the same applies to index_together
.
如果您只有一组唯一字段,则可以对unique_together使用一维可迭代。但是,文档并未指出同样适用于index_together。
This would also be okay:
这也没关系:
class Meta(object):
unique_together = 'foo', 'bar'
index_together = [
['foo', 'bar']
]
This, however, is NOT supported by the documentation:
但是,文档不支持此操作:
class Meta(object):
unique_together = 'foo', 'bar'
index_together = 'foo', 'bar'
#4
1
According to the docs, it will only enforce uniqueness on database level. I think generally making a field unique does not imply it has an index. Though you could also simply check on db level if the index exists. Everything indicates though it does not.
根据文档,它只会强制数据库级别的唯一性。我认为通常使字段唯一并不意味着它有一个索引。虽然如果索引存在,您也可以简单地检查数据库级别。一切都表明虽然没有。