I have 3 classes, like so:
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
class Order(Base):
__tablename__ = 'orders'
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey(User.id))
user = relationship(User, backref=backref('orders'))
#user = relationship(User, backref=backref('orders', lazy='subquery'))
class Item(Base):
__tablename__ = 'items'
id = Column(Integer, primary_key=True)
order_id = Column(Integer, ForeignKey(Order.id))
order = relationship(Order, backref=backref('items'))
#order = relationship(Order, backref=backref('items', lazy='joined'))
The commented out variations of the relationships are the working ones,
which allow me to do
results = session.query(User).all()
and have it grab the users, then the join between the orders and items in a
second subquery.
I have been trying to reproduce this behavior using the per-entity default
loading strategies described
at
http://docs.sqlalchemy.org/en/latest/orm/loading_relationships.html#per-entity-default-loading-strategies
but have been unable to get the same behavior.
This works:
session.query(User).options(
Load(User).subqueryload('orders').joinedload('items')
)
But I am trying to build something programmatically, so I'm hoping to avoid
the chaining. What I want to work, but does not, is this:
session.query(User).options(
Load(User).subqueryload('orders'),
Load(Order).joinedload('items'),
)
I'm trying to have each class able to return its own load strategy options,
which i could then feed into options.
Is it possible to use the Load(...) system to replicate the behavior of the
lazy attribute provided to a relationship, as in, when the query is
constructed, it behaves _exactly_ as if the value provided to Load(...) was
actually set as the 'lazy' keyword of the attribute? I poked around in the
source a bit, and with my failed attempt, JoinedLoader.__init__ is never
even called. The subquery is issued, but is not joined against anything.
I'm not sure how to make this work.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.