[Sqlalchemy-tickets] Issue #3590: Eagerload is ignored if root object is already in session (zzzeek
Brought to you by:
zzzeek
|
From: Milas B. <iss...@bi...> - 2015-11-18 19:44:07
|
New issue 3590: Eagerload is ignored if root object is already in session https://bitbucket.org/zzzeek/sqlalchemy/issues/3590/eagerload-is-ignored-if-root-object-is Milas Bowman: I have a relationship defaulted to `lazy='noload'` and on specific queries override this using `.options(eagerload())`. However, if the object being queried is already loaded into the session, it will not perform the eager load if queried. This is a regression in 1.0.9 -- it works properly in 1.0.8. I'm specifically seeing it in a unit test using SQLite in-memory database. Here's a simple example (also attached): ``` #!python from __future__ import print_function from sqlalchemy import Column, Integer, ForeignKey, String, create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import eagerload, relationship, sessionmaker Base = declarative_base() engine = create_engine('sqlite:///:memory:', echo=True) Session = sessionmaker(bind=engine) class Foo(Base): __tablename__ = 'foo' id = Column(Integer, primary_key=True) value = Column(String(50)) bars = relationship('Bar', back_populates='foo', lazy='noload') class Bar(Base): __tablename__ = 'bar' id = Column(Integer, primary_key=True) foo_id = Column(ForeignKey('foo.id')) foo = relationship('Foo', back_populates='bars', lazy='noload') def initialize(): Base.metadata.create_all(engine) session = Session() f = Foo(value='foobar') b = Bar(foo=f) session.add(b) session.commit() session.close() if __name__ == '__main__': initialize() session = Session() bar = session.query(Bar).one() print('Foo should be None - ', bar.foo) bar_with_foo_query = session.query(Bar).options(eagerload(Bar.foo)) bar_missing_foo = bar_with_foo_query.one() print('Foo is incorrectly None - ', bar_missing_foo.foo) # invalidate the session cache to force it to reload session.close() bar_with_foo = bar_with_foo_query.one() print('Foo is loaded correctly - ', bar_with_foo.foo) ``` Let me know if there's any additional information I can provide. |