2017-03-06 29 views
0

我喜欢记录事件中发生的所有查询,我想调试它们或稍后在其上运行解释计划。如何在使用stmt.compile打印查询时抑制SQLAlchemy警告?

例如:

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" 

有什么办法来抑制这些警告?值得注意的是,我只想在记录/打印查询这个非常具体的情况下压制他们,而不是全局压制。

回答

0

可以抑制SQL炼金术警告消息在Python 2.7,像这样:

import warnings 
from sqlalchemy import exc as sa_exc 

with warnings.catch_warnings(): 
    warnings.simplefilter("ignore", category=sa_exc.SAWarning) 
    results = query.execute()