On 09/08/2010 01:05 PM, Jack Kordas wrote:
> When I try to use both aliases and labels, the results are not named
> as expected.
>
> Instead of being able to access the columns as <label-name>_<column-
> name> it appears as <original-table-name>_<numeric-sequence>_<column-
> name>
>
> Thanks,
>   Jack
>
> Sample code follows:
>
> parent = Table('parent', metadata,
>     Column('id', INTEGER(), primary_key=True),
>     Column('name', VARCHAR(length=128)),
>     Column('first_id', INTEGER(),
>         ForeignKey(u'child.id')),
>   )
>
> child = Table('child', metadata,
>     Column('id', INTEGER(), primary_key=True),
>     Column('name', VARCHAR(length=128))
>   )
>
> def test_labels1(conn):
>     s = select([parent,child], use_labels=True)
>     s = s.where(parent.c.first_id==child.c.id)
>     return conn.execute(s).fetchone()
>
> def test_alias1(conn):
>     firstchild = child.alias()
>     s = select([parent,firstchild], use_labels=True)
>     s = s.where(parent.c.first_id==firstchild.c.id)
>     return conn.execute(s).fetchone()
>
> conn = engine.connect()
>
> results = test_labels1(conn)
> print results.parent_name
> print results.child_name
>
> results = test_alias1(conn)
> print 'alias1 results: '
> print results.parent_name
> #print results.firstchild_name # expected this to work
> print results.child_1_name # this worked instead
>   

You need to set an explicit name for the alias to prevent SQLAlchemy
from generating an anonymous name[1]:

firstchild = child.alias("firstchild")

-Conor

[1]
http://www.sqlalchemy.org/docs/core/expression_api.html#sqlalchemy.sql.expression.FromClause.alias

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to