如何使用stmt.compile打印查询时禁止SQLAlchemy警告?

问题描述 投票:0回答:1

我想记录我想要调试它们的事件中发生的所有查询,或者稍后对它们运行解释计划。

例如:

from sqlalchemy import select
from sqlalchemy.dialect import oracle
queries = {}
# ...
sel = select([foo.c.id, foo.c.bar])
queries['foo query'] = sel.compile(dialect=oracle.dialect(), 
                                   compile_kwargs={'literal_binds': True})
results = conn.execute(select)

这总是最终输出以下警告:

SAWarning: Textual column expression 'id' should be explicitly declared
 with text('location'), or use column('location') for more specificity (this
 warning may be suppressed after 10 occurrences)
if guess_is_literal else "column"

有没有办法压制这些警告?值得注意的是,我只想在记录/打印查询的特定情况下抑制它们,而不是全局抑制。

python python-2.7 sqlalchemy
1个回答
0
投票

您可以在Python 2.7中禁止SQL Alchemy警告消息,如下所示:

import warnings
from sqlalchemy import exc as sa_exc

with warnings.catch_warnings():
    warnings.simplefilter("ignore", category=sa_exc.SAWarning)
    results = query.execute()
© www.soinside.com 2019 - 2024. All rights reserved.