SQLAlchemy: undefer column via joinedload technique
<p>Here is my query.</p>

<pre><code>query = dbsession.query(Parent)\
    .options(joinedload(Parent.child))\
    .first()
</code></pre>

<p>Model <code>Child</code> is available through <code>Parent.child</code> relationship and has deffered column named 'column'. How to undefer and load that column using query listed above?</p>

<p>Chain the undefer option through the joined load with</p>

<pre><code>joinedload(Parent.child).undefer('column')
</code></pre>

<p>See <a href="http://docs.sqlalchemy.org/en/latest/orm/loading_relationships.html#loading-along-paths" rel="nofollow">"Loading Along Paths"</a> and the documentation on <a href="http://docs.sqlalchemy.org/en/latest/orm/query.html#sqlalchemy.orm.strategy_options.Load" rel="nofollow">loaders</a>.</p>

<p>Given the following models:</p>

<pre><code>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')
   ...:
</code></pre>

<p>undeferring <code>value</code></p>

<pre><code>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
</code></pre>

<p>without</p>

<pre><code>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
</code></pre>

python sqlalchemy