Django南迁移 - 添加FULLTEXT索引

Gen*_*cos 20 mysql migration django django-south

我需要在我的Django模型的某个字段中添加一个FULLTEXT索引,并了解没有内置功能来执行此操作,并且必须在mysql(我们的后端数据库)中手动添加这样的索引.

我希望在每个环境中创建此索引.我理解模型更改可以用Django南迁移来处理,但有没有办法在迁移过程中添加这样的FULLTEXT索引?

通常,如果需要运行任何自定义SQL,我如何将其作为迁移的一部分.

谢谢.

Yuj*_*ita 32

您可以将任何内容编写为迁移.这才是重点!

一旦你South启动和运行,键入python manage.py schemamigration myapp --empty my_custom_migration创建一个空白的迁移,你可以自定义.

打开XXXX_my_custom_migration.py文件myapp/migrations/并在forwards方法中键入自定义SQL迁移.例如,您可以使用db.execute

迁移可能如下所示:

class Migration(SchemaMigration):

    def forwards(self, orm):
        db.execute("CREATE FULLTEXT INDEX foo ON bar (foobar)")
        print "Just created a fulltext index..."
        print "And calculated {answer}".format(answer=40+2)


    def backwards(self, orm):
        raise RuntimeError("Cannot reverse this migration.") 
        # or what have you


$ python manage.py migrate myapp XXXX # or just python manage.py migrate.
"Just created fulltext index...."
"And calculated 42"
Run Code Online (Sandbox Code Playgroud)