print query _mapper_zero_or_none()

135 views
Skip to first unread message

Rick Otten

unread,
Jun 24, 2015, 10:25:57 AM6/24/15
to sqlal...@googlegroups.com
I've been using the "literalquery" function as described in the top answer on this Stackoverflow page for the past year or so:  http://stackoverflow.com/questions/5631078/sqlalchemy-print-the-actual-query

I found it very easy to add extra types to it.  It has been really handy.  I use it in practically every SQLAlchemy development project that ends up involving complicated queries.  The more complicated the query, the more likely I am to really need this capability.

It helps to troubleshoot the query and to run explain plans in the database for optimizing the query as well.

Lastly, I use it to provide an audit trail for what queries were run, and when.

--


Recently I started getting this error:

statement._mapper_zero_or_none()
AttributeError: 'Query' object has no attribute '_mapper_zero_or_none'

I'm thinking something changed in one of the 1.0.x releases that removed or broke the _mapper_zero_or_none function.  Is that true? (or did I break something else in my script that tis error is hiding?)   I have not found anything that even describes what this function does in the online documentation or by Google or DuckDuckGo searching.  I'll search the source next, I suppose, but thought I'd ask and get the question out there in case anyone else bumps into the same issue.

@zzzeek states that this particular function is unnecessary in the comments on the stackoverflow discussion, but doesn't really explain why or how to remove it:

"""this solution in no way accurately represents what's involved to stringify a statement. the reaching intio _mapper_Zero_or_none and attempts to locate "bound" metadata are all completely unnecessary. The overriding of Compiler is also a hack that overlooks documented APIs. I'll provide an answer shortly. – zzzeek May 23 '14 at 17:42"""

I don't have a stackoverflow account with enough points to be able to comment and ask the question there.

--
Rick.


Mike Bayer

unread,
Jun 24, 2015, 1:13:38 PM6/24/15
to sqlal...@googlegroups.com


On 6/24/15 10:25 AM, Rick Otten wrote:
I've been using the "literalquery" function as described in the top answer on this Stackoverflow page for the past year or so:  http://stackoverflow.com/questions/5631078/sqlalchemy-print-the-actual-query

I found it very easy to add extra types to it.  It has been really handy.  I use it in practically every SQLAlchemy development project that ends up involving complicated queries.  The more complicated the query, the more likely I am to really need this capability.

It helps to troubleshoot the query and to run explain plans in the database for optimizing the query as well.

Lastly, I use it to provide an audit trail for what queries were run, and when.

--


Recently I started getting this error:

statement._mapper_zero_or_none()
AttributeError: 'Query' object has no attribute '_mapper_zero_or_none'

I'm thinking something changed in one of the 1.0.x releases that removed or broke the _mapper_zero_or_none function.  Is that true? (or did I break something else in my script that tis error is hiding?)   I have not found anything that even describes what this function does in the online documentation or by Google or DuckDuckGo searching.  I'll search the source next, I suppose, but thought I'd ask and get the question out there in case anyone else bumps into the same issue.

that method was a private method and was never safe to use.  The public equivalent is the column_descriptions function: http://docs.sqlalchemy.org/en/rel_1_0/orm/query.html?highlight=column_descriptions#sqlalchemy.orm.query.Query.column_descriptions



@zzzeek states that this particular function is unnecessary in the comments on the stackoverflow discussion, but doesn't really explain why or how to remove it:

"""this solution in no way accurately represents what's involved to stringify a statement. the reaching intio _mapper_Zero_or_none and attempts to locate "bound" metadata are all completely unnecessary. The overriding of Compiler is also a hack that overlooks documented APIs. I'll provide an answer shortly. – zzzeek May 23 '14 at 17:42"""


Rick Otten

unread,
Jun 24, 2015, 1:46:32 PM6/24/15
to sqlal...@googlegroups.com
Thanks!

For now I just changed _mapper_zero_or_none to _mapper_zero.  It seems to work ok for my short term need.

I'll put together a new function, using the approach you recommend, when I get time. 
Reply all
Reply to author
Forward
0 new messages