Skip to content Skip to sidebar Skip to footer

Sqlalchemy: Undefer Column Via Joinedload Technique

Here is my query. query = dbsession.query(Parent)\ .options(joinedload(Parent.child))\ .first() Model Child is available through Parent.child relationship and has deffered

Solution 1:

Chain the undefer option through the joined load with

joinedload(Parent.child).undefer('column')

See "Loading Along Paths" and the documentation on loaders.

Given the following models:

In [3]: class A(Base):
   ...:     __tablename__ = 'a'
   ...:     id = Column(Integer, primary_key=True)
   ...:     

In [4]: class B(Base):
   ...:     __tablename__ = 'b'
   ...:     id = Column(Integer, ForeignKey('a.id'), primary_key=True)
   ...:     value = deferred(Column(Integer))
   ...:     a = relationship('A', backref='bs')
   ...:

undeferring value

In [21]: print(session.query(A).options(joinedload(A.bs).undefer('value')))SELECT a.id AS a_id, b_1.value AS b_1_value, b_1.id AS b_1_id 
FROM a LEFT OUTER JOIN b AS b_1 ON a.id = b_1.id

without

In [17]: print(session.query(A).options(joinedload(A.bs)))
SELECT a.id AS a_id, b_1.id AS b_1_id 
FROM a LEFT OUTER JOIN b AS b_1 ON a.id = b_1.id

Post a Comment for "Sqlalchemy: Undefer Column Via Joinedload Technique"