After some more testing (the use of loggers in sqlalchemy helped alot
:)) i have narrowed the issue to backreference creation - they seem to
be always created lazy, even if relation is set to eager. Here's a
simple test script (not in unittest format unfortunately as i'm not
sure how to make one that would work within sqlalchemy unittest
framework).

from sqlalchemy import *
dburi = "put your own here"

import logging
#logging.getLogger("sqlalchemy.orm.mapper.Mapper").setLevel(1)
#logging.getLogger("sqlalchemy.orm.strategies.EagerLoader").setLevel(1)
#uncomment if you want to see what exactly is going on

engine = create_engine(dburi, strategy='plain', echo=True)
metadata = BoundMetaData(engine)

middle = Table('middle', metadata,
    Column('id', Integer, primary_key = True, autoincrement=True),
    Column('data', String(50)),
)

left = Table('left', metadata,
    Column('id', Integer, ForeignKey(middle.c.id), primary_key=True),
    Column('tag', String(50), primary_key=True),
)

right = Table('right', metadata,
    Column('id', Integer, ForeignKey(middle.c.id), primary_key=True),
    Column('tag', String(50), primary_key=True),
)

class Middle(object):
    def __init__(self, data): self.data = data
class Left(object):
    def __init__(self, data): self.tag = data
class Right(object):
    def __init__(self, data): self.tag = data

mapper(Left, left)
mapper(Right, right)
mapper(Middle, middle, properties = {
    'left': relation(Left, private=True, lazy=False, backref='middle',
        foreignkey=left.c.id,  primaryjoin = left.c.id==middle.c.id ),
    'right': relation(Right, private=True, lazy=False,
backref='middle',
        foreignkey=right.c.id,  primaryjoin = right.c.id==middle.c.id
),
    }
)

def make_data():
    session = create_session(bind_to=engine)
    p = Middle('test1')
    p.left.append(Left('tag1'))
    p.right.append(Right('tag2'))
    session.save(p)
    session.flush()

def test(close_session):
    session = create_session(bind_to=engine)
    obj = session.query(Left).get_by(tag='tag1')
    if close_session:
        session.close()
    print obj.middle.right[0]

if __name__=="__main__":
    metadata.create_all()
    try:
        make_data()
        test(False) # will work fine
        test(True) # will fail
    finally:
        metadata.drop_all()


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
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