nutship
nutship

Reputation: 4924

Using db_index with ForeignKey

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?

Upvotes: 0

Views: 2062

Answers (1)

marcelor
marcelor

Reputation: 92

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.

Upvotes: 3

Related Questions