问题描述
我需要在我的 Django 模型的一个字段中添加 FULLTEXT 索引,并且了解没有内置功能可以执行此操作,并且必须在 mysql(我们的后端数据库)中手动添加这样的索引.
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).
我希望在每个环境中都创建此索引.我知道模型更改可以处理 Django 南迁移,但是有没有办法可以添加这样的 FULLTEXT 索引作为迁移的一部分?
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?
一般来说,如果有任何自定义 SQL 需要运行,我如何将其作为迁移的一部分.
In general, if there is any custom SQL that needs to be run, how can I make it a part of a migration.
谢谢.
推荐答案
你可以写任何东西作为迁移.这就是重点!
You can write anything as a migration. That's the point!
South
启动并运行后,输入 python manage.py schemamigration myapp --empty my_custom_migration
以创建可以自定义的空白迁移.
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.
在 myapp/migrations/
中打开 XXXX_my_custom_migration.py
文件,并在 forwards
方法中输入您的自定义 SQL 迁移.例如,您可以使用 db.execute
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
迁移可能如下所示:
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 南迁移 - 添加 FULLTEXT 索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!