Bitten By SqlAlchemy 0.6 Truthiness

After upgrading to SqlAlchemy 0.6, I was bitten by this
known gotcha.

Given that st is some SqlAlchemy Table (as defined below):

import logging
from sqlalchemy import *
summary = Table(
    'summary',metadata,
    Column('country',String)
)
st = summary_table.c
query = select(st).where(st.country=='US')

Then we try to log using it

logging.critical('%r', query)

we get this uninformative error:

TypeError: Boolean value of this clause is not defined

What is going on here? It seems that as part of logging, the logger
must evaluate the truthiness of each argument to the logging call, as shown
in this part of the (wrapped) traceback:

/usr/lib64/python2.4/logging/__init__.pyc in __init__
    (self, name, level, pathname, lineno, msg, args, exc_info)
    224         # For the use case of passing a dictionary, this should not be a
    225         # problem.
--> 226         if args and (len(args) == 1) and args[0] and (type(args[0]) == types.DictType):
    227             args = args[0]
    228         self.args = args

As the Gotcha point out, while the stringiness of a Query is unchanged,
the truthiness is now in question. Luckily, the fix is simple (if hackish):

logging.critical('%s', str(query))
Advertisements