24
class Comments(models.Model):
content = models.ForeignKey(Content)
是否需要將db_index添加到「content」?或者會自動被索引,因爲它是一個外鍵?我是否需要將db_index添加到此Django模型?
class Comments(models.Model):
content = models.ForeignKey(Content)
是否需要將db_index添加到「content」?或者會自動被索引,因爲它是一個外鍵?我是否需要將db_index添加到此Django模型?
除非另有規定,否則將爲ForeignKey
創建索引。相關源代碼:
class ForeignKey(RelatedField, Field):
# snip
def __init__(self, to, to_field=None, rel_class=ManyToOneRel, **kwargs):
# snip
if 'db_index' not in kwargs:
kwargs['db_index'] = True
要禁用在ForeignKey上創建索引,請設置`db_index = False`:http://code.djangoproject.com/ticket/13730 – 2014-02-03 14:10:18