<p dir="auto"><strong>Migrated issue, originally created by Patrick Rusk</strong></p>
<p dir="auto">(This problem was noted in comments in <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/zzzeek/sqlalchemy/commit/3fa38a1a2313b4644daa431d629394d6bb14497a/hovercard" href="https://github.com/zzzeek/sqlalchemy/commit/3fa38a1a2313b4644daa431d629394d6bb14497a">zzzeek/sqlalchemy@<tt>3fa38a1</tt></a> but not followed up on.)</p>
<p dir="auto">The <code class="notranslate">sqlalchemy.orm.Query()</code> class takes an <code class="notranslate">entities</code> 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.</p>
<p dir="auto">The offending line is <a href="https://github.com/zzzeek/sqlalchemy/blob/master/lib/sqlalchemy/orm/query.py#L150">https://github.com/zzzeek/sqlalchemy/blob/master/lib/sqlalchemy/orm/query.py#L150</a></p>
<p dir="auto">As noted in the comments, changing it to <code class="notranslate">if entities is not None:</code> would probably fix the issue.</p>
<p dir="auto">Test case:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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)"><pre class="notranslate"><code class="notranslate">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)
</code></pre></div>
<p dir="auto">...works under 1.2.7, but generates this stack trace under 1.2.8...</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-1-cf50a8343d19&gt; in &lt;module&gt;()
      8 
      9 select = sqlalchemy.sql.func.max(User.id).label('max')
---&gt; 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 = {}
--&gt; 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
--&gt; 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):
--&gt; 487         raise TypeError(&quot;Boolean value of this clause is not defined&quot;)
    488 
    489     __nonzero__ = __bool__

TypeError: Boolean value of this clause is not defined"><pre class="notranslate"><code class="notranslate">---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-1-cf50a8343d19&gt; in &lt;module&gt;()
      8 
      9 select = sqlalchemy.sql.func.max(User.id).label('max')
---&gt; 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 = {}
--&gt; 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
--&gt; 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):
--&gt; 487         raise TypeError("Boolean value of this clause is not defined")
    488 
    489     __nonzero__ = __bool__

TypeError: Boolean value of this clause is not defined
</code></pre></div>
<p dir="auto">Anyone experiencing this can easily fix their issue by throwing [] around their expression, but it is a breaking change (of an undocumented feature).</p>
<p dir="auto">(Please forgive the nonsensical test case above. I've never used SQLAlchemy. Just diagnosing a bug in someone else's code.)</p>
<p dir="auto"><strong>Migrated issue, originally created by Charles-Axel Dein (<a href="https://github.com/charlax">@charlax</a>)</strong></p>
<p dir="auto">I'm upgrading a codebase to sqlalchemy 1.0.0beta5  from 0.9.9.</p>
<p dir="auto">It seems that version 1 removed the ability to be forgiving about mentioning a join twice:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="session.query(Toaster).join(Toast).join(Toast).all()"><pre class="notranslate"><code class="notranslate">session.query(Toaster).join(Toast).join(Toast).all()
</code></pre></div>
<p dir="auto">This will raise with postgres: <code class="notranslate">ProgrammingError: (psycopg2.ProgrammingError) table name "toasts" specified more than once</code></p>
<p dir="auto">The same query run with sqla 0.9.9 runs fine, because I guess it removes the duplicate join.</p>
<p dir="auto">I'm not sure that can be considered a regression given that joins should not be mentioned more than once, but it's definitely a different behavior from 0.9.9</p>