since i was working on this anyway, i added a feature that will make
this possible, although i want to improve upon it.
what you can do right now is:
class User(object):pass
class Address(object):pass
mapper(User, user_table, properties={
'addresses':relation(Address, lazy=False)
})
mapper(Address, address_table)
selectquery = users.outerjoin(addresses).select(use_labels=True)
q = create_session().query(User)
result = q.options(contains_eager('addresses')).instances
(selectquery.execute())
I think from this I can see a way to make it so that by default you
wouldnt need the contains_eager() option, and it would work the way
you expect (i.e., no aliasing by default). but this is something you
can try for now in the trunk.
On Oct 18, 2006, at 9:54 PM, James Taylor wrote:
>
> I expected to be able to map both sides of the relationship from a
> resultset with the proper columns. However it appears to still lazy
> load all the child objects. Short example (works against trunk):
>
> import pkg_resources
> pkg_resources.require( "sqlalchemy" )
> pkg_resources.require( "pysqlite" )
>
> from sqlalchemy import *
>
> # Setup classes, tables, mappers
>
> metadata = BoundMetaData( 'sqlite:////tmp/test.db' )
> metadata.engine.echo = True
>
> class A( object ):
> pass
>
> class B( object ):
> pass
>
> A.table = Table( "table_a", metadata,
> Column( "id", Integer, primary_key=True),
> Column( "name", String(20) ) )
>
> B.table = Table( "table_b", metadata,
> Column( "id", Integer, primary_key=True),
> Column( "name", String(20) ),
> Column( "table_a_id", Integer, ForeignKey( "table_a.id" ) ) )
>
> mapper( B, B.table )
> mapper( A, A.table, properties=dict( bs=relation( B, backref="a" ) ) )
>
> metadata.create_all()
>
> # Insert some stuff
>
> session = create_session()
>
> for i in range( 10 ):
> a = A()
> a.name = "A_%d" % i
> session.save( a )
> for j in range( 10 ):
> b = B()
> b.name= "B_%d" % i
> b.a = a
> session.save( b )
> session.flush()
> session.clear()
>
> # Map from select
>
> results = select( [ A.table, B.table ], A.c.id == B.c.table_a_id,
> use_labels=True ).execute()
> some_as = class_mapper( A ).instances( results, session, with_options=
> [eagerload('bs')] )
>
> # At this point many queries against 'b_table'
>
> for a in some_as:
> list( a.bs )
>
> >
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---