Friday, 27 September 2013

How do I iterate through this sqlalchemy query?

How do I iterate through this sqlalchemy query?

Relative newbie to python and even newer to sqlalchemy. I have the following:
Without a subquery, when I join 2 tables without a subquery each row
returned is essentially 2 objects:
query = DBSession.query(Table1,Table2).outerjoin(Table2,Table1.id==Table2.id)
for row in query:
# returns (<myproject.models.Table1 object at
0x3ad2e50>,<myproject.models.Table2 object at 0x3ad2e50>)
With a subquery, the behavior changes: subquery =
DBSession.query(Table1).order_by(Table1.d.desc()).subquery() query =
DBSession.query(subquery,Table2).outerjoin(Table2,subquery.c.id==Table2.id).group_by(subquery.c.id)
for row in query:
# hoping for 2 objects (<myproject.models.Table1 object at
0x3ad2e50>,<myproject.models.Table2 object at 0x3ad2e50>)
# receiving (1,'Dave Thomas',10001,<myproject.models.Table1 object at
0x3ad2e50>)
How can I get 2 objects for my subquery above? The columns of Table1 may
change someday and thus I don't know what position will be at in the
tuple.
thanks!

No comments:

Post a Comment