Ray*_*oal 4 postgresql full-text-search sqlalchemy alembic
我在 Postgres 表上手动创建了一个 GIN 索引,如下所示:
create index idx_ib_keywords on stuff using gin(to_tsvector('english'::regconfig, keywords));
Run Code Online (Sandbox Code Playgroud)
它被创建得很好:
\d info_block_template
Table info_block_template
Column | Type | Collation | Nullable | Default
------------+--------+-----------+----------+--------
.
.
.
keywords | text | | not null |
.
.
.
Indexes:
.
. "idx_ib_keywords" gin (to_tsvector('english'::regconfig, keywords))
Run Code Online (Sandbox Code Playgroud)
现在,我正在使用 alembic 进行迁移。当我使用 alembic 自动生成迁移时,不会自动生成 GIN 索引。不用担心,自动发电机不应该是完美的。所以我想进去手工编辑迁移文件。
我已经搜索了如何做到这一点,我能找到的最接近的是我关注的这个页面,并写道
op.create_index(op.f('idx_ib_keywords'), 'stuff', ['keywords'], postgresql_using='gin')
Run Code Online (Sandbox Code Playgroud)
进入我的迁移脚本。当我应用此迁移时,出现错误:
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedObject) 数据类型文本没有用于访问方法“gin”的默认运算符类 提示:您必须为索引指定运算符类或为数据类型定义默认运算符类。
这是一个很好的错误信息;它告诉我我需要做这to_tsvector件事。但是,我不知道如何在 SQLAlchemy 中做到这一点。
有没有一种简单的方法可以在 SQLAlchemy 中编写它,还是应该放入迁移文件中的原始 SQL?
事实证明,附加信息被指定为功能索引,而不是postgresql_usingkwarg 的一部分。
正确的 SQLAlchemy 语句是:
op.create_index(op.f('idx_ib_keywords'),
'info_block_template',
[sa.text('to_tsvector(\'english\'::regconfig, keywords)')],
postgresql_using='gin')
Run Code Online (Sandbox Code Playgroud)
应用此迁移时,新创建的索引将完全按照需要显示:
"idx_ib_keywords" gin (to_tsvector('english'::regconfig, keywords))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1573 次 |
| 最近记录: |