Note that there are some explanatory texts on larger screens.

plurals
  1. POCan SQLAlchemy eager/joined loads be suppressed once set up?
    text
    copied!<p>I've got a case where most of the time the relationships between objects was such that pre-configuring an eager (joined) load on the relationship made sense. However now I've got a situation where I really don't want the eager load to be done.</p> <p>Should I be removing the joined load from the relationship and changing all relevant queries to join at the query location (ick), or is there some way to suppress an eager load in a query once it is set up?</p> <p>Below is an example where eager loading has been set up on the User->Address relationship. Can the query at the end of the program be configured to NOT eager load?</p> <pre><code>import sqlalchemy as sa from sqlalchemy.ext.declarative import declarative_base import sqlalchemy.orm as orm ##Set up SQLAlchemy for declarative use with Sqlite... engine = sa.create_engine("sqlite://", echo = True) DeclarativeBase = declarative_base() Session = orm.sessionmaker(bind = engine) class User(DeclarativeBase): __tablename__ = "users" id = sa.Column(sa.Integer, primary_key = True, autoincrement = True) name = sa.Column(sa.String, unique = True) addresses = orm.relationship("Address", lazy = "joined", #EAGER LOAD CONFIG IS HERE ) def __init__(self, Name): self.name = Name class Address(DeclarativeBase): __tablename__ = "addresses" id = sa.Column(sa.Integer, primary_key = True, autoincrement = True) address = sa.Column(sa.String, unique = True) FK_user = sa.Column(sa.Integer, sa.ForeignKey("users.id")) def __init__(self, Email): self.address = Email ##Generate data tables... DeclarativeBase.metadata.create_all(engine) ##Add some data... joe = User("Joe") joe.addresses = [Address("joe@example.com"), Address("joeyjojojs@example.net")] s1 = Session() s1.add(joe) s1.commit() ## Access the data for the demo... s2 = Session() #How to suppress the eager load (auto-join) in the query below? joe = s2.query(User).filter_by(name = "Joe").one() # &lt;-- HERE? for addr in joe.addresses: print addr.address </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload