将db_index与外键一起使用

时间:2013-11-22 15:51:45

标签: python django indexing

我只是想保持安全。我有这个型号:

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
     ...

db_index的使用是否正确?而且,无需为playerAplayerB添加额外的索引?

1 个答案:

答案 0 :(得分:1)

Django自动为所有models.ForeignKey列创建索引。只需运行“./manage.py sql appname”,您就会看到用于创建索引的sql语句。