Hi everybody,

In my db-setup, there is a one-to-many relationship with a backref that is
configured for eager loading (not the relation but the backref!). When I
access the child objects of the parent object, the query from sqlalchemy
joins back to the parent object (due to eager loading). In this case, the
join is unnecessary, since the parent object is already known.

Is there something I am missing or a way to avoid the join?

Cheers and thanks,
Martin



Simple test case:



from sqlalchemy import *
from sqlalchemy.orm import mapper, sessionmaker, relation, backref

engine = create_engine("postgres://testuser:testu...@localhost:5432/quicktest",
echo=True)
metadata = MetaData()

users_table = Table('users', metadata,
    Column('id', Integer, primary_key=True),
    Column('name', String(64)),
)

keywords_table = Table('keywords', metadata,
    Column('id', Integer, primary_key=True),
    Column('user_id', Integer, ForeignKey("users.id")),
    Column('keyword', String(64))
)


class User(object):
    def __init__(self, name):
        self.name = name

class Keyword(object):
    def __init__(self, keyword):
        self.keyword = keyword


mapper(User, users_table, properties={
       'keywords': relation(Keyword, backref=backref('user', lazy=False))})
mapper(Keyword, keywords_table)


metadata.drop_all(engine)
metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()

user1 = User('kirk')
keyword1 = Keyword('enterprise')
keyword2 = Keyword('captain')

user1.keywords = [keyword1, keyword2]

session.add(user1)
session.commit()

queried_user1 = session.query(User).filter_by(name='kirk').one()
print queried_user1
print "--------------------------------------------------------------\n"

# This is where the unnecessary join happens
print queried_user1.keywords
print "--------------------------------------------------------------\n"

--~--~---------~--~----~------------~-------~--~----~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to