The get_by, selectone_by, selectfirst_by methods append limit 1 to the
sql query. If you are eagerloading children, this causes the children
to not all be loaded.
Example:
from sqlalchemy import *
engine=create_engine('sqlite:///:memory:',echo=True )
dbmeta=BoundMetaData(engine)
test=Table ( 'test', dbmeta ,
Column ( 'id', Integer, primary_key=True ) )
class Test(object): pass
mapper(Test,test)
children=Table ( 'children', dbmeta,
Column ( 'id', Integer, primary_key=True ),
Column ( 'parent_id', Integer, ForeignKey ( 'test.id'),
nullable=False ) )
class Child(object):pass
mapper(Child,children,properties={'parent':relation(Test,backref='children')})
dbmeta.create_all()
s=create_session()
t=Test()
t.id=1
for i in range(3):
c=Child()
c.parent_id=1
t.children.append(c)
s.save(t)
s.flush()
s.clear()
# eagerload children
t=s.query(Test).options(eagerload('children')).get_by(id=1)
for c in t.children:
print 'Found child %d' % c.id
# only one child is loaded
Here is the relevant output:
2006-10-31 11:21:58,131 INFO
sqlalchemy.engine.base.ComposedSQLEngine.0x..74 SELECT test.id AS
test_id, children_ebf.parent_id AS children_ebf_parent_id,
children_ebf.id AS children_ebf_id
FROM test LEFT OUTER JOIN children AS children_ebf ON test.id =
children_ebf.parent_id
WHERE test.id = ? ORDER BY test.oid, children_ebf.oid
LIMIT 1 OFFSET 0
2006-10-31 11:21:58,135 INFO
sqlalchemy.engine.base.ComposedSQLEngine.0x..74 [1]
Found child 1
Replacing get_by with get or select returns the correct output.
Should this behavior be changed or should it not be possible to
eagerload children with those methods?
-Dennis
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---