hand-constructing the query used by an eagerload is not terribly  
possible at the moment.  if you look at the generated SQL using  
echoing, the joined tables in an eagerload are "aliased" (to avoid  
conflicts with multiple eager loads against the same table, WHERE  
criterion that uses the same table, etc), and youd need to know the  
exact alias name (which is randomly generated when an eager loading  
query compiles for the first time) in order to use the right column  
names.  to get at the Alias object being used is tricky because its  
deeply embedded, not generated until you call select() from the Query  
object, etc.

there is a fuzzy plan to add a feature whereby you can tell instances 
() the names of your own columns that will be used in a hand- 
constructed eager loading query, which would entail replacing the  
"EagerRowAdapter" that gets used internally with your own version (or  
just a default version that doesnt alias the columns).  need to work  
out an interface for that.


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

Reply via email to