<p dir="auto"><strong>Migrated issue, originally created by Marco Martinez (<a href="https://github.com/marcomartinez">@marcomartinez</a>)</strong></p>
<p dir="auto">I'm trying to get <code class="notranslate">column_descriptions</code> from a query that I'm building that contains multiple joins to the same target, but I'm getting the following error message:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;bug-alias.py&quot;, line 37, in test
    query.column_descriptions
  File &quot;/home/mmartinez/env/sa/lib/python2.7/site-packages/sqlalchemy/orm/query.py&quot;, line 2583, in column_descriptions
    for ent in self._entities
  File &quot;/home/mmartinez/env/sa/lib/python2.7/site-packages/sqlalchemy/orm/util.py&quot;, line 398, in __getattr__
    raise AttributeError(key)
AttributeError: entity"><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File "bug-alias.py", line 37, in test
    query.column_descriptions
  File "/home/mmartinez/env/sa/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2583, in column_descriptions
    for ent in self._entities
  File "/home/mmartinez/env/sa/lib/python2.7/site-packages/sqlalchemy/orm/util.py", line 398, in __getattr__
    raise AttributeError(key)
AttributeError: entity
</code></pre></div>
<p dir="auto">Here's an example of what my code pre-1.0 was doing:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.declarative import declarative_base

Session = orm.Session()

Base = declarative_base()


class User(Base):
    __tablename__ = 'user'
    id = sa.Column(sa.Integer(), primary_key=True)
    name = sa.Column(sa.Unicode(), nullable=False, unique=True)


class MyData(Base):
    __tablename__ = 'mydata'

    id = sa.Column(sa.Integer(), primary_key=True)

    data = sa.Column(sa.Unicode(), nullable=False, unique=True)

    created_by_id = sa.Column(sa.ForeignKey(User.id))
    created_by = orm.relationship(User, foreign_keys=[created_by_id])

    modified_by_id = sa.Column(sa.ForeignKey(User.id))
    modified_by = orm.relationship(User, foreign_keys=[modified_by_id])

Creator = orm.aliased(User)
Modifier = orm.aliased(User)

query = (
    Session.query(
        MyData.data,
        sa.literal(u'why').label('someliteral'),   # Literals will fail too
        Creator.name.label('created_by'),
        Modifier.name.label('modfied_by')
    )
    .join(Creator, MyData.created_by)
    .join(Modifier, MyData.modified_by)
)

print ([d['name'] for d in query.column_descriptions])                                  "><pre class="notranslate"><code class="notranslate">import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.declarative import declarative_base

Session = orm.Session()

Base = declarative_base()


class User(Base):
    __tablename__ = 'user'
    id = sa.Column(sa.Integer(), primary_key=True)
    name = sa.Column(sa.Unicode(), nullable=False, unique=True)


class MyData(Base):
    __tablename__ = 'mydata'

    id = sa.Column(sa.Integer(), primary_key=True)

    data = sa.Column(sa.Unicode(), nullable=False, unique=True)

    created_by_id = sa.Column(sa.ForeignKey(User.id))
    created_by = orm.relationship(User, foreign_keys=[created_by_id])

    modified_by_id = sa.Column(sa.ForeignKey(User.id))
    modified_by = orm.relationship(User, foreign_keys=[modified_by_id])

Creator = orm.aliased(User)
Modifier = orm.aliased(User)

query = (
    Session.query(
        MyData.data,
        sa.literal(u'why').label('someliteral'),   # Literals will fail too
        Creator.name.label('created_by'),
        Modifier.name.label('modfied_by')
    )
    .join(Creator, MyData.created_by)
    .join(Modifier, MyData.modified_by)
)

print ([d['name'] for d in query.column_descriptions])                                  
</code></pre></div>
<p dir="auto">Reading the 1.0 changlog, I tried this instead (which worked) :</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="query = (
    Session.query(
        MyData.data,
        sa.literal(u'why').label('someliteral'),
    )
    .join(MyData.created_by, aliased=True)
    .add_column(User.name.label('created_by'))
    .reset_joinpoint()
    .join(MyData.modified_by, aliased=True)
    .add_column(User.name.label('modified_by'))
    .reset_joinpoint()
)

print ([d['name'] for d in query.column_descriptions]) 
"><pre class="notranslate"><code class="notranslate">query = (
    Session.query(
        MyData.data,
        sa.literal(u'why').label('someliteral'),
    )
    .join(MyData.created_by, aliased=True)
    .add_column(User.name.label('created_by'))
    .reset_joinpoint()
    .join(MyData.modified_by, aliased=True)
    .add_column(User.name.label('modified_by'))
    .reset_joinpoint()
)

print ([d['name'] for d in query.column_descriptions]) 

</code></pre></div>
<p dir="auto">Am I missing something? Thanks in advance.</p>
<p dir="auto"><strong>Migrated issue, originally created by Pavel Brych</strong></p>
<p dir="auto">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.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#!/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(&quot;postgresql://user:passwd@localhost/db_name&quot;, echo=True)
Base.metadata.create_all(e)

session = Session(e)

user = aliased(User,name=&quot;user&quot;)

q = session.query(user.username.label(&quot;username&quot;))

print(q.column_descriptions)"><pre class="notranslate"><code class="notranslate">#!/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)
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;/home/pavel/python/hores/src/sqlalchemytest.py&quot;, line 27, in &lt;module&gt;
    print(q.column_descriptions)
  File &quot;/usr/lib64/python3.4/site-packages/sqlalchemy/orm/query.py&quot;, line 2583, in column_descriptions
    for ent in self._entities
  File &quot;/usr/lib64/python3.4/site-packages/sqlalchemy/orm/query.py&quot;, line 2583, in &lt;listcomp&gt;
    for ent in self._entities
  File &quot;/usr/lib64/python3.4/site-packages/sqlalchemy/orm/util.py&quot;, line 398, in __getattr__
    raise AttributeError(key)
AttributeError: entity"><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File "/home/pavel/python/hores/src/sqlalchemytest.py", line 27, in &lt;module&gt;
    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 &lt;listcomp&gt;
    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
</code></pre></div>