Can SQLAlchemy eager/joined loads be suppressed once set up?
You can use Query.options(raiseload('*'))
or Query.enable_eagerloads(False)
.
Query.enable_eagerloads(False)
will disable all eager loading on the query. That is, even if you put a joinedload()
or something, it won't be executed.
Query.options(raiseload('*'))
will install a raiseload
loader on every column, making sure they're not lazily loaded: an exception is raised instead. Note that this mode is fine for development and testing environments, but may be destructive in production. Make it optional like this:
Query.options(raiseload('*') if development else defaultload([]))
also note that raiseload('*')
only works for top-level relationships. It won't spread on joined entities! If you request a relationship, you have to specify it twice:
session.query(User).options(
load_only('id'),
joinedload(User.addresses).options(
load_only('id'),
raiseload('*')
),
raiseload('*')
)
also, raiseload('*')
only works for relationships, not columns :)
For columns, use defer(..., raiseload=True)
You may override eagerness of properties on query-by-query basis, as far as I remember. Will this work?
from sqlalchemy.orm import lazyload
joe = (s2.query(User)
.options(lazyload('addresses'))
.filter_by(name = "Joe").one())
for addr in joe.addresses:
print addr.address
See the docs.