【问题标题】:Do I need to add a db_index to this Django model?我需要在这个 Django 模型中添加一个 db_index 吗?
【发布时间】:2011-06-17 16:54:36
【问题描述】:
class Comments(models.Model):
    content = models.ForeignKey(Content)

我需要将 db_index 添加到“内容”吗?还是会因为它是外键而自动编入索引?

【问题讨论】:

    标签: python mysql database django indexing


    【解决方案1】:

    除非另有说明,否则将为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
    

    【讨论】:

    猜你喜欢
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    相关资源
    最近更新 更多