question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

orm.Query() constructor (used w/ undocumented behavior) raising "Boolean value of this clause is not defined" exception

See original GitHub issue

Migrated 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:closed
  • Created 5 years ago
  • Comments:13

github_iconTop GitHub Comments

1reaction
sqlalchemy-botcommented, Nov 27, 2018

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.

  1. 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).

  2. 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.

0reactions
sqlalchemy-botcommented, Nov 27, 2018

Changes by Michael Bayer (@zzzeek):

  • changed status to closed
Read more comments on GitHub >

github_iconTop Results From Across the Web

SA 1.0.2 throwing TypeError: Boolean value of this clause is not ...
This error is being thrown on code that worked with 0.9.8. It seems to be checking a comparison on something, but I can't...
Read more >
SQLAlchemy - TypeError: Boolean value of this clause is not ...
I want the model to set its result field based on the values of the home_score and away_score fields. I've worked a lot...
Read more >
1.4 Changelog — SQLAlchemy 2.0 Documentation
Fixed issue where the underlying DBAPI cursor would not be closed when using the Query object as an iterator, if a user-defined exception...
Read more >
Database Engine events and errors - SQL Server
Consult this MSSQL error code list to find explanations for error messages for SQL Server database engine events.
Read more >
What's New — pandas 0.19.1 documentation
Bug in DataFrame.insert where multiple calls with duplicate columns can fail (GH14291); pd.merge() will raise ValueError with non-boolean parameters in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found