Django south migration - 添加FULLTEXT索引 [英] Django south migration - Adding FULLTEXT indexes

查看:204
本文介绍了Django south migration - 添加FULLTEXT索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的一个Django模型的字段中添加一个FULLTEXT索引,并且明白没有内置的功能来完成此操作,并且必须在mysql(我们的后端DB)中手动添加这样一个索引。



我希望在每个环境中创建这个索引。我明白模型的变化可以解决Django南迁,但是有一种方法可以在迁移过程中添加一个FULLTEXT索引?



一般来说,如果有任何需要运行的自定义SQL,那么我如何才能将其作为迁移的一部分。



谢谢。

解决方案

你可以写任何东西作为迁移。这是要点!



一旦你有 South 开始运行,输入 python manage .py schemamigration myapp --empty my_custom_migration 创建一个可以自定义的空白迁移。



打开 XXXX_my_custom_migration 。$ myapp / migrations / 中的.py 文件,然后在转发中输入您的自定义SQL迁移方法。例如,您可以使用 db.execute



迁移可能如下所示:

  class迁移(SchemaMigration):

def forward(self,orm):
db.execute(CREATE FULLTEXT INDEX foo ON bar(foobar))
print刚刚创建全文索引...
print并计算{answer}格式(answer = 40 + 2)


def backwards(self,orm):
raise RuntimeError(Can not reverse this migration。)
#或者你有什么


$ python manage.py migrate myapp XXXX#或者只是python manage.py migrate。
刚刚创建的全文索引....
并计算42


I need to add a FULLTEXT index to one of my Django model's fields and understand that there is no built in functionality to do this and that such an index must be added manually in mysql (our back end DB).

I want this index to be created in every environment. I understand model changes can be dealt with Django south migrations, but is there a way I could add such a FULLTEXT index as part of a migration?

In general, if there is any custom SQL that needs to be run, how can I make it a part of a migration.

Thanks.

解决方案

You can write anything as a migration. That's the point!

Once you have South up and running, type in python manage.py schemamigration myapp --empty my_custom_migration to create a blank migration that you can customize.

Open up the XXXX_my_custom_migration.py file in myapp/migrations/ and type in your custom SQL migration there in the forwards method. For example you could use db.execute

The migration might look something like this:

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"

这篇关于Django south migration - 添加FULLTEXT索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆