25
class Comments(models.Model):
    content = models.ForeignKey(Content)

Do I need to add a db_index to "content"? Or would that automatically be indexed because it's a foreign key?

1 Answer 1

44

Unless specified otherwise, an index will be created for a ForeignKey. Relevant source code:

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
Sign up to request clarification or add additional context in comments.

1 Comment

To disable creation of an index on a ForeignKey, set db_index=False: code.djangoproject.com/ticket/13730

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.