我正在将 Flask-Migrate 用于一个项目,其中目标数据库是与 MySQL 兼容的 Aurora。
我使用Menzhuo's SQLAlchemy fulltext search module向模型添加了全文索引:
class Post(db.Model, FullText, Serializable):
__tablename__ = 'post'
__fulltext_columns__ = ('description', 'text')
id = db.Column(db.String(10), primary_key=True)
description = db.Column(db.String(256))
.
.
.
现在,当我使用 Flask-Migrate 生成迁移时,不会自动获取全文索引。我很清楚 Flask-Migrate 并不能拾取所有内容,您必须手动向迁移脚本中添加一些内容。
例如,我知道如何手动插入诸如
之类的行op.add_column(...)
op.create_unique_constraint(...)
op.create_index(...)
在生成的迁移中的 upgrade
方法内部。然而,当我查看create_index
documentation for Alembic时我没有看到任何对创建全文索引的支持。我看到了唯一索引的 unique
参数,但没有看到全文索引。
那么我是否缺少直接在 Alembic 中执行此操作的方法(可能使用 SQLAlchemy 命令)?或者我必须直接编写 SQL 吗?我不愿意做后者,而且我以前从未这样做过。如果有必要的话,它是如何工作的?
最佳答案
摘自 create_index()
上的 Alembic 文档我们发现
- **kw – Additional keyword arguments not mentioned above are dialect specific, and passed in the form
<dialectname>_<argname>
. See the documentation regarding an individual dialect at Dialects for detail on documented arguments.
然后在 MySQL 方言文档中 "Index Prefixes" 下:
MySQL storage engines permit you to specify an index prefix when creating an index. SQLAlchemy provides this feature via the
mysql_prefix
parameter onIndex
:Index('my_index', my_table.c.data, mysql_prefix='FULLTEXT')
鉴于上述情况,您的 Alembic 迁移应该像这样定义索引:
op.create_index(..., mysql_prefix='FULLTEXT')
关于python - 如何在 Alembic 迁移中指定全文搜索索引 (MySQL)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50439408/