Django south migration - Adding FULLTEXT indexes

旧巷老猫 提交于 2019-12-04 16:41:44

问题


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.


回答1:


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"


来源:https://stackoverflow.com/questions/5465336/django-south-migration-adding-fulltext-indexes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!