Django south migration - Adding FULLTEXT indexes(Django 南迁移 - 添加 FULLTEXT 索引)
问题描述
我需要在我的 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 索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Django 南迁移 - 添加 FULLTEXT 索引
基础教程推荐
- 无法解决整理冲突 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
