Django: Est-ce que unique_together implique db_index = True de la même manière que le fait ForeignKey?
-
27-10-2019 - |
Question
A champ sur un modèle, foo = models.ForeignKey(Foo)
ajoutera automatiquement un index de base de données pour la colonne, afin de faire paraître-ups plus rapidement. C'est bon et bien, mais les docs de Django ne le font pas état si les champs dans le unique_together
d'un modèle méta reçoivent le même traitement. J'arrive d'avoir un modèle dans lequel un champ char qui figure dans unique_together
nécessite un index pour les recherches rapides. Je sais que ce sera tout pas mal d'ajouter un db_index=True
double dans la définition du champ, mais je suis curieux.
La solution
unique_together
does not automatically add indexes for each field included in the list.
The new versions of Django suggest using Index & constraint meta options instead:
https://docs.djangoproject.com/en/2.2/ref/models/options/#unique-together
https://docs.djangoproject.com/en/2.2/ref/models/options/#index-together
Autres conseils
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.
If unique_together
does add an index, it will be a multiple column index.
If you want one of the columns to be indexed individually, I believe you need to specify db_index=True
in the field definition.
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:
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
.
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'
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.