Add descending indexes in MySQL 8.0 via peewee migrate

Viewed 27

I am working on a MySQL database. I first create a table by peewee, then insert all records, and the final step is creating indexes for columns. Based on this question, I know I can use peewee migrate to create index after the table has been created.

For example, let's say I have a table as below

import peewee as pw
import playhouse.migrate

db = pw.MySQLDatabase(...)
migrator = playhouse.migrate.MySQLMigrator(db)

class User(pw.Model):
    username = pw.CharField()
    userval = pw.FloatField()
    class Meta:
        database = db

db.create_tables([User])
# insert all records
...

# add index
with db.atomic():
    playhouse.migrate.migrate(
        migrator.add_index('User', ('userval',)),
    )

I can successfully create the index for this single column "userval", but my question is how to create a descending index for this column? Since the add_index only accepts strings, I can't use User.userval.desc(), then how to pass the keyword DESC to the migrator?

Or there is actually no way to create the descending index via peewee, and I have to login into MySQL, and then create the index by

CREATE INDEX userval_index ON User (userval DESC)
1 Answers

Did you try:

with db.atomic():
    playhouse.migrate.migrate(
        migrator.add_index('User', ('userval DESC',)),
    )
Related