【问题标题】:How do I specify an index for a TextField in Django with a MySql backend?如何在带有 MySql 后端的 Django 中为 TextField 指定索引?
【发布时间】:2012-06-23 03:58:04
【问题描述】:

我在 Django 中定义了一个模型,看起来(部分)如下所示:

class Info(models.Model):
    information = models.TextField(max_length = 32, null = True, db_index = True)

但是,当我尝试使用 MySql 后端的 syncdb 时,我得到 Failed to install index for app.Info model: (1170, "BLOB/TEXT column 'information' used in key specification without a key length")

我该如何解决这个问题?

【问题讨论】:

标签: mysql django django-models


【解决方案1】:

答案是将字段指定为CharField,而不是TextField

class Info(models.Model):
    information = models.CharField(max_length = 32, null = True, db_index = True)

【讨论】:

  • CharField 在这里就足够了,max_length = 32,但有时需要创建没有长度限制的文本字段(mysql 中的 TEXT),但仅对索引的字符数有限制。
猜你喜欢
  • 2020-01-08
  • 2011-12-24
  • 2023-03-21
  • 2021-09-13
  • 2018-10-30
  • 2016-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多