Search code examples
pythonpython-3.xormmysql-pythonpeewee

how to specify peewee TextField index key length


How can i set the key length for TextField with peewee python orm. I am on python3.7 and i get this error message:

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

I tried specifying it like this:

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

However that does not seem to work since it returns this:

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

Solution

  • Try adding the index explicitly:

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

    Please note that specifying indexes on text fields in mysql is probably a bad idea. If you know the length ahead-of-time, probably better off just using a CharField() in that case.