orm.Query() constructor (used w/ undocumented behavior) raising "Boolean value of this clause is not defined" exception
See original GitHub issueMigrated issue, originally created by Patrick Rusk
(This problem was noted in comments in https://github.com/zzzeek/sqlalchemy/commit/3fa38a1a2313b4644daa431d629394d6bb14497a but not followed up on.)
The sqlalchemy.orm.Query()
class takes an entities
parameter defined to be “a sequence of entities and/or SQL expressions”, but it used to actual accept a single SQL expression as well. Now, some such expressions cause a “TypeError: Boolean value of this clause is not defined” exception to be raised.
The offending line is https://github.com/zzzeek/sqlalchemy/blob/master/lib/sqlalchemy/orm/query.py#L150
As noted in the comments, changing it to if entities is not None:
would probably fix the issue.
Test case:
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer
import sqlalchemy.sql
class User(declarative_base()):
__tablename__ = 'users'
id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
select = sqlalchemy.sql.func.max(User.id).label('max')
query = sqlalchemy.orm.Query(select)
…works under 1.2.7, but generates this stack trace under 1.2.8…
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-cf50a8343d19> in <module>()
8
9 select = sqlalchemy.sql.func.max(User.id).label('max')
---> 10 query = sqlalchemy.orm.Query(select)
/Users/patrick/.virtualenvs/shackleton/lib/python2.7/site-packages/sqlalchemy/orm/query.pyc in __init__(self, entities, session)
140 self.session = session
141 self._polymorphic_adapters = {}
--> 142 self._set_entities(entities)
143
144 def _set_entities(self, entities, entity_wrapper=None):
/Users/patrick/.virtualenvs/shackleton/lib/python2.7/site-packages/sqlalchemy/orm/query.pyc in _set_entities(self, entities, entity_wrapper)
148 self._primary_entity = None
149 self._has_mapper_entities = False
--> 150 if entities:
151 for ent in util.to_list(entities):
152 entity_wrapper(self, ent)
/Users/patrick/.virtualenvs/shackleton/lib/python2.7/site-packages/sqlalchemy/sql/elements.pyc in __bool__(self)
485
486 def __bool__(self):
--> 487 raise TypeError("Boolean value of this clause is not defined")
488
489 __nonzero__ = __bool__
TypeError: Boolean value of this clause is not defined
Anyone experiencing this can easily fix their issue by throwing [] around their expression, but it is a breaking change (of an undocumented feature).
(Please forgive the nonsensical test case above. I’ve never used SQLAlchemy. Just diagnosing a bug in someone else’s code.)
Issue Analytics
- State:
- Created 5 years ago
- Comments:13
Patrick Rusk wrote:
Michael, I don’t disagree with any of your analysis, and I think my original description pretty much laid out the same. Good change to the title. I’ll merely note a couple things.
It is arguably more pythonic to allow one expression to be passed in by itself, rather than requiring that it be wrapped in a sequence. I don’t feel strongly about such things, nor do I know enough about SQLAlchemy to know if that fits in with the feel of the library. But a case could be made to support it and change the documentation (since it has effectively been supported up until now).
If you decide that behavior should explicitly not be allowed, I would recommend not changing it in a patch release, but adding deprecation warnings to the next few release notes and changing it in a major release that explicitly removes deprecated functionality (even undocumented).
Thanks for your attention. I appreciate it.
Changes by Michael Bayer (@zzzeek):