On 10/21/15 9:19 PM, Kristi Tsukida wrote:
> It looks like only one aliased table gets aliased correctly when joining
> to multiple aliased tables.
>
> Test case:
>
> from __future__ import print_function
> from sqlalchemy import Integer, String, select, Date, and_
> from sqlalchemy import Column
>
> from sqlalchemy.ext.declarative import declarative_base
> from sqlalchemy.orm.util import aliased
>
> Base = declarative_base()
>
> class Users(Base):
> __tablename__ = "users"
> id = Column(Integer, primary_key=True)
> name = Column(String)
> date = Column(Date)
>
> a = aliased(Users, name="aaa")
> b = aliased(Users, name="bbb")
> c = aliased(Users, name="ccc")
>
> sel = select([a.name, b.name]).select_from(
> c.__table__.join(
> a.__table__,
> a.name == c.name,
> ).join(
> b.__table__,
> b.date == c.date,
> )
> )
>
> print(sel)
the __table__ element of an aliased() object is the original table. So
your query is this:
..select_from(Users.__table__...).join(Users.__table__...)
use standalone join() when dealing with ORM entities and don't use
__table__ for querying, it has a very different meaning than the entity
itself, even for the non-aliased version:
from sqlalchemy import join
sel = select([a.name, b.name]).select_from(
join(c, a, a.name == c.name).join(
b,
b.date == c.date,
)
)
SELECT aaa.name, bbb.name
FROM users AS ccc JOIN users AS aaa ON aaa.name = ccc.name JOIN users AS
bbb ON bbb.date = ccc.date
>
> Output:
> SELECT aaa.name, bbb.name
> FROM users AS bbb, users JOIN users ON aaa.name = ccc.name JOIN users ON
> bbb.date = ccc.date
>
> I expect there to be three "AS" clauses in this sql, but there is only
> one, e.g.
> SELECT aaa.name, bbb.name
> FROM users AS ccc JOIN users AS aaa ON aaa.name = ccc.name JOIN users AS
> bbb ON bbb.date = ccc.date
>
> Using sqlalchemy 1.0.9, python 3.4.3
>
> --
> You received this message because you are subscribed to the Google
> Groups "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [email protected]
> <mailto:[email protected]>.
> To post to this group, send email to [email protected]
> <mailto:[email protected]>.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.