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
-~----------~----~----~----~------~----~------~--~---