[Sqlalchemy-tickets] Issue #4269: orm.Query() constructor raising "Boolean value of this clause is
Brought to you by:
zzzeek
From: Patrick R. <iss...@bi...> - 2018-06-01 14:13:25
|
New issue 4269: orm.Query() constructor raising "Boolean value of this clause is not defined" exception https://bitbucket.org/zzzeek/sqlalchemy/issues/4269/ormquery-constructor-raising-boolean-value 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: ``` #!python 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... ``` #!python --------------------------------------------------------------------------- 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.) |