gpt4 book ai didi

python - 如何在 Alembic 迁移中指定全文搜索索引 (MySQL)?

转载 作者:太空宇宙 更新时间:2023-11-04 02:25:46 27 4
gpt4 key购买 nike

我正在将 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.

然后在 "Index Prefixes" 下的 MySQL 方言文档中:

MySQL storage engines permit you to specify an index prefix when creating an index. SQLAlchemy provides this feature via the mysql_prefix parameter on Index:

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com