Issue 1:
Migrated issue, originally created by Marco Martinez (@marcomartinez)

I'm trying to get column_descriptions from a query that I'm building that contains multiple joins to the same target, but I'm getting the following error message:

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

Here's an example of what my code pre-1.0 was doing:

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])

Reading the 1.0 changlog, I tried this instead (which worked) :

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])

Am I missing something? Thanks in advance.

Issue 2:
Migrated issue, originally created by Pavel Brych

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