Hi! SQLAlchemy is great! I have a question regarding create_session() and
lazy loading (using version 0.2.2). See comments in code. Could anyone
explain this strange behavior to me? Thanks!

-Peter

from sqlalchemy import *

class Address(object):

    pass
    
class Customer(object):

    pass

def test():
    engine = create_engine("sqlite:///:memory:")
    meta = BoundMetaData(engine)
    customer_table = Table("customer", meta,
        Column("id", Integer, primary_key=True),
        Column("name", String),
        Column("address", Integer, ForeignKey("address.id")))
    address_table = Table("address", meta,
        Column("id", Integer, primary_key=True),
        Column("street", String))
    meta.create_all()
    address_table.insert().execute(id=1, street="Blabla 7")
    address_table.insert().execute(id=2, street="Blabla 9")
    customer_table.insert().execute(id=1, name="Mr. Smith", address=1)
    customer_table.insert().execute(id=2, name="Mr. Brown", address=2)

    Address.mapper = mapper(Address, address_table)
    Customer.mapper = mapper(Customer, customer_table, properties=dict(
        addressobj = relation(Address, lazy=True)))

    # this does not work using lazy loading (why?), but eager does
    for c in create_session().query(Customer).select():
        print c.id, c.name, c.addressobj

    # however, assigning session to a variable first makes lazy loading work
    session = create_session()
    for c in session.query(Customer).select():
        print c.id, c.name, c.addressobj
    
if __name__ == "__main__":
    test()

_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to