How do you limit/specify the columns that are loaded via a relationship()?

78 views
Skip to first unread message

Seth

unread,
May 10, 2014, 6:39:42 PM5/10/14
to sqlal...@googlegroups.com
Is there any way to get SQLAlchemy to only load specific columns through a relationship()?

For example, with this scenario:

class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    children = relationship("Child", lazy='joined')

class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('parent.id'))
    cheap_column = Column(Unicode(10))
    expensive_column = Column(LargeBinary)

I'd like Child to get lazily joined when Parent is loaded, but only with Child's "cheap_column" and not it's "expensive_column".

Thanks,
Seth

Michael Bayer

unread,
May 10, 2014, 10:59:33 PM5/10/14
to sqlal...@googlegroups.com
session.query(Parent).options(defaultload(“children”).load_only(“cheap_column"))

or really if you want to cut out “expensive_column”

session.query(Parent).options(defaultload(“children”).defer(“expensive_column”))




--
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 sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Seth

unread,
May 14, 2014, 1:39:45 PM5/14/14
to sqlal...@googlegroups.com
Thanks Mike,

But...is there no way to set this behavior directly on the "relationship()"? The whole point there is to be "lazy" ;)

Seth

Michael Bayer

unread,
May 14, 2014, 5:35:27 PM5/14/14
to sqlal...@googlegroups.com


there are deferred() columns that you can set on a mapper but there’s not a mechanism right now to set deferred target attributes when the object is loaded only from a specific relationship without the specific call within the query().    So unless you maybe ran those options into every Query unconditionally, or something.  I’d look to see how big a deal it is to just be explicit on this one.

Seth

unread,
May 14, 2014, 7:49:02 PM5/14/14
to sqlal...@googlegroups.com
Ok, thanks so much for your help.

Jonathan Vanasco

unread,
May 15, 2014, 1:42:12 PM5/15/14
to sqlal...@googlegroups.com
Michael-

Quick question for clarity...

I have a table with a few deferred columns.  If I want to "eagerly" load them during a query, I should pass in the "undefer" option right ?

Michael Bayer

unread,
May 15, 2014, 2:24:51 PM5/15/14
to sqlal...@googlegroups.com
yeah there’s some bad history with the API here, in that you can’t easily pass them all at once, but right now it’s  q.options(undefer(‘*’)) for everything or q.options(undefer(‘x’), undefer(‘y’), …).  or undefer_group() if you’ve applied a group to them.



Jonathan Vanasco

unread,
May 15, 2014, 2:54:19 PM5/15/14
to sqlal...@googlegroups.com

that's not too bad.  at least for me; i only defer a few HSTORE columns.
Reply all
Reply to author
Forward
0 new messages