0.9.x - 1.0.x regression - AttributeError: entity when accessing query.column_descriptions when using label() with attribute of aliased entity

Issue #3409 resolved
Pavel Brych created an issue

After upgrading to 1.0.2 resp. 1.0.3 SQLAlchemy, this error popped up. It happens when label() is used with attribute of aliased entity.

#!/usr/bin/python
# -*- coding: utf-8 -*-
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import aliased

Base = declarative_base()

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    username = Column(String(255))
    realname = Column(String(255))

e = create_engine("postgresql://user:passwd@localhost/db_name", echo=True)
Base.metadata.create_all(e)

session = Session(e)

user = aliased(User,name="user")

q = session.query(user.username.label("username"))

print(q.column_descriptions)
Traceback (most recent call last):
  File "/home/pavel/python/hores/src/sqlalchemytest.py", line 27, in <module>
    print(q.column_descriptions)
  File "/usr/lib64/python3.4/site-packages/sqlalchemy/orm/query.py", line 2583, in column_descriptions
    for ent in self._entities
  File "/usr/lib64/python3.4/site-packages/sqlalchemy/orm/query.py", line 2583, in <listcomp>
    for ent in self._entities
  File "/usr/lib64/python3.4/site-packages/sqlalchemy/orm/util.py", line 398, in __getattr__
    raise AttributeError(key)
AttributeError: entity

Comments (7)

  1. Mike Bayer repo owner
    • Repaired / added to tests yet more expressions that were reported as failing with the new 'entity' key value added to :attr:.Query.column_descriptions, the logic to discover the "from" clause is again reworked to accommodate columns from aliased classes, as well as to report the correct value for the "aliased" flag in these cases. fixes #3409

    → <<cset 24d6ea362e75>>

  2. Mike Bayer repo owner
    • Repaired / added to tests yet more expressions that were reported as failing with the new 'entity' key value added to :attr:.Query.column_descriptions, the logic to discover the "from" clause is again reworked to accommodate columns from aliased classes, as well as to report the correct value for the "aliased" flag in these cases. fixes #3409

    (cherry picked from commit 24d6ea362e757c79b3bada663cf6fc9f262dae4f)

    → <<cset aee58e21631c>>

  3. Log in to comment