SqlAlchemy 会话被定义为:
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
并被配置:
DBSession.configure(bind=engine)
Base.metadata.bind = engine
并且需要使用在事务中执行原始 sql 为 jsonb 字段创建索引:
with transaction.manager:
sql = "CREATE INDEX my_index ON my_table USING gin ((jsonb_field -> 'jsonb_key'));"
DBSession.execute(sql)
该操作的 SqlAlchemy 日志如下所示:
BEGIN (implicit)
INFO CREATE INDEX my_index ON my_table USING gin ((jsonb_field -> 'jsonb_key'));
INFO {}
INFO ROLLBACK
并且没有创建索引。
但是相同的 SQL 命令psql
创建索引没有错误:
=> CREATE INDEX my_index ON my_table USING gin ((jsonb_field -> 'jsonb_key'));
=> CREATE INDEX
在这种情况下如何使用 SqlAlchemy“执行”命令创建索引?