This example can't work in this sequence, or this query can't be run
and mapped?
If I wanted to execute this query:
SELECT inner_query.test_id AS inner_query_test_id,
inner_query.test_other_id AS inner_query_test_other_id,
inner_query.test_active AS inner_query_test_active,
inner_query.test2_id AS inner_query_test2_id,
inner_query.test2_other_id AS inner_query_test2_other_id,
inner_query.test2_active AS inner_query_test2_active
FROM (SELECT test.id AS test_id, test.other_id AS test_other_id,
test.active AS test_active, test2.id AS test2_id, test2.other_id AS
test2_other_id, test2.active AS test2_active
FROM test JOIN test2 ON test2.other_id = test.id) AS inner_query

is my only option to do this query through text and build out my own
column_labels dictionary?

On Dec 14, 8:06 am, Michael Bayer <[EMAIL PROTECTED]> wrote:
> this example as is cant really work in any version because the query
> doesnt know to map the "s2" alias to the mappers that its using, so
> you have to explicitly connect them.  0.3 has more limited capability
> to do this.
>
> anyway in 0.4 you can do it like this:
>
> print
> session
> .query
> (Test
> ).options
> (contains_alias(s2)).add_entity(Test2,alias=s2).instances(s2.execute())
>
> and if youre on trunk also like this:
>
> print session.query(Test).select_from(s2).add_entity(Test2,
> alias=s2).all()
>
> On Dec 14, 2007, at 3:30 AM, dykang wrote:
>
> > from sqlalchemy import *
> > from sqlalchemy.orm import *
>
> > metadata = MetaData("mysql://mrlabs:[EMAIL PROTECTED]:3306/test")
> > metadata.bind.echo=True
>
> > table = Table("test", metadata,
> >        Column("id", Integer, primary_key=True),
> >        Column("other_id", Integer),
> >        Column("active", Boolean))
>
> > table2 = Table("test2", metadata,
> >        Column("id", Integer, primary_key=True),
> >        Column("other_id", Integer),
> >        Column("active", Boolean))
> > table.create()
> > table.insert().execute([{"other_id":1, "active": False}, {"other_id":
> > 2, "active": True}])
>
> > table2.create()
> > table2.insert().execute([{"other_id":1, "active": False}, {"other_id":
> > 2, "active": True}])
>
> > test = table.select(table.c.active).alias("test")
> > class Test(object):
> >    pass
>
> > class Test2(object):
> >    pass
>
> > mapper(Test, table)
> > mapper(Test2, table2)
>
> > session = create_session()
> > q = join(table, table2, table2.c.other_id == table.c.id)
> > s = q.select(use_labels=True).alias("inner_query")
> > s2 = s.select(use_labels=True).alias("outer_query")
>
> > print session.query(Test,
> > Test2).instances(s2.execute(use_labels=True))
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to