On 12/03/2015 06:16 AM, Ofir Herzas wrote: > load_only as stated > in http://docs.sqlalchemy.org/en/latest/orm/loading_columns.html does > the following: > "An arbitrary set of columns can be selected as “load only” columns, > which will be loaded *while deferring all other columns* on a given > entity..."
Hi Ofir - As it says, "on a given entity", an entity refers to a class-oriented object. A relationship refers to a different set of entities so these must be referred to independently. The "load_only" function in that code example is hyperlinked, which is intended to encourage the reader to click and read through the more detailed documentation for this function which is at: http://docs.sqlalchemy.org/en/rel_1_0/orm/loading_columns.html?highlight=load_only#sqlalchemy.orm.load_only > > However, joined relationships are not affected by this, meaning that the > produced sql still joins all 'joined' relationships > > Consider the following example: > > | > importsqlalchemy assa > fromsqlalchemy.ext.declarative importdeclarative_base > > Base=declarative_base() > > classFoo(Base): > __tablename__ ='foo' > id =sa.Column(sa.Integer,primary_key=True) > bars =sa.orm.relationship("Bar",lazy="joined") > > classBar(Base): > __tablename__ ='bar' > id =sa.Column(sa.Integer,primary_key=True) > foo_id =sa.Column(sa.Integer,sa.ForeignKey('foo.id')) > baz =sa.Column(sa.Integer) > > e =sa.create_engine("sqlite://",echo=True) > session =sa.orm.Session(e) > session.query(Foo).options(sa.orm.load_only('id')).all() > | > > The query produced is the following: > | > SELECT foo.id AS foo_id,bar_1.id AS bar_1_id,bar_1.foo_id AS > bar_1_foo_id,bar_1.baz AS bar_1_baz > FROM foo LEFT OUTER JOIN bar AS bar_1 ON foo.id =bar_1.foo_id > | > > I understand that I can use the lazyload option to prevent the join but > I thought it would be nice if the load_only handled it out-of-the-box... there's "out of the box" and there's "assuming a specific and arbitrary intent" - you have lazy="joined" in your mapping so if you don't want that to occur you need to tell the query to turn it off, load_only('a', 'b').lazyload('bars'). > > -- > 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] > <mailto:[email protected]>. > To post to this group, send email to [email protected] > <mailto:[email protected]>. > Visit this group at http://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/d/optout. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
