【问题标题】:how to specify peewee TextField index key length如何指定peewee TextField索引键长度
【发布时间】:2020-01-08 17:11:01
【问题描述】:

如何使用 peewee python orm 设置 TextField 的密钥长度。我在 python3.7 上,我收到此错误消息:

peewee.InternalError: (1170, "BLOB/TEXT column 'text' used in key specification without a key length")

我尝试这样指定它:

text = TextField(unique = True, key_length = 255, index = True)

但是这似乎不起作用,因为它返回了这个:

TypeError: __init__() got an unexpected keyword argument 'key_length'

【问题讨论】:

    标签: python python-3.x orm mysql-python peewee


    【解决方案1】:

    尝试显式添加索引:

    class Note(Model):
        content = TextField()
        class Meta:
            indexes = (
                SQL('create index note_content on note (content(100))'),
            )
    

    请注意,在 mysql 中为文本字段指定索引可能是个坏主意。如果您提前知道长度,那么在这种情况下最好只使用 CharField()。

    【讨论】:

    • 感谢 peewee :),也许我可以在之后根据我拥有的所有数据计算索引的密钥长度,然后再创建密钥长度索引
    猜你喜欢
    • 1970-01-01
    • 2020-02-20
    • 2013-10-04
    • 2022-11-06
    • 1970-01-01
    • 2018-04-11
    • 2012-06-23
    • 2013-05-08
    • 1970-01-01
    相关资源
    最近更新 更多