0

I just want to be on the safe side. I have this models:

class Player(models.Model):
     name = models.CharField(max_length=64, db_index=True)
     ...  

class Match(models.Model):
     date = models.DateField()
     playerA = models.ForeignKey(Player, related_name='playerA')   # neither here db_index
     playerB = models.ForeignKey(Player, related_name='playerB')   # nor here db_index
     ...

Is the usage of db_index correct? And, there is no need to add extra indexes to either playerA or playerB?

2
  • 1
    Yes, db_index won't case errors if that's what you mean, otherwise what is your question? Commented Nov 22, 2013 at 16:06
  • No errors I know, I rather want to know whether this is applied properly and will speed up filtering through playerA and playerB filters, or do I have to additionally set db_index on playerA and playerB fields in the model? Commented Nov 22, 2013 at 16:57

1 Answer 1

3

Django automatically creates an index for all models.ForeignKey columns. Just run "./manage.py sql appname" and you'll see the sql statements for the creation of the indexes.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Does it mean db_index in Player.name from my example is not needed at all?
Exactly. db_index is only needed in a ForeignKey when you want to disable the automatic index creation, in that case you use db_index=False

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.