I am trying to create a model with unique index.
My models look like that:
class ModelA(Model):
FieldA = CharField()
class ModelB(Model):
fieldA = TextField(unique=True)
fieldB = CharField(max_length=10)
fieldC = BooleanField(default=False)
fieldD = ForeignKeyField(ModelA)
And I'm creating the tables like this:
db.create_tables([ModelA, ModelB])
My problem is that after creating the table there's no index on ModelB.fieldA.
When I changed ModelB.fieldA type to CharField instead of TextField:
class ModelA(Model):
FieldA = CharField()
class ModelB(Model):
fieldA = CharField(unique=True)
fieldB = CharField(max_length=10)
fieldC = BooleanField(default=False)
fieldD = ForeignKeyField(ModelA)
It works properly and an index is created for ModelB.fieldA.
Is there any reason I can't create a unique index on TextField field type? How can I create a unique index on ModelB.fieldA and keeping it's type TextField?
Thanks
MySQL requires you to specify a prefix for indexes on text type: https://dev.mysql.com/doc/refman/8.0/en/create-index.html#create-index-column-prefixes
So you will want an index like:
CREATE INDEX my_index ON my_model(text_column (100));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.