all eager loading strategies are fully supported for all relationship configurations except for those which use the "dynamic" loader strategy.
see the comments in the example below for tips on diagnosing your issue.
from sqlalchemy import Column
from sqlalchemy import create_engine
from sqlalchemy import ForeignKey
from sqlalchemy import inspect
from sqlalchemy import Integer
from sqlalchemy import select
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.orm import Session
Base = declarative_base()
class Bonus(Base):
__tablename__ = 'bonus'
id = Column(Integer, primary_key=True)
class Accomplishment(Base):
__tablename__ = 'accomplishment'
id = Column(Integer, primary_key=True)
bonus_id = Column(ForeignKey('
bonus.id'))
e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)
s = Session(e)
s.add(Bonus(basis=Accomplishment()))
s.commit()
s.close()
# echo=True shows SQL emitted, which includes the eager load:
# FROM bonus LEFT OUTER JOIN accomplishment AS accomplishment_1 ON
bonus.id = accomplishment_1.bonus_id
bonus_w_accom = s.execute(select(Bonus)).scalar()
# we can see object was eagerly loaded like this, among other ways
assert 'basis' in bonus_w_accom.__dict__
# close the session, will detach all objects. note we have not
# called session.rollback() or session.commit(). These will expire
# the attributes. expire_on_commit=False will prevent commit() from doing so.
s.close()
# object is detached
assert inspect(bonus_w_accom).detached
# attribute is still there
assert bonus_w_accom.basis
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.