3 messages in com.googlegroups.sqlalchemy[sqlalchemy] Re: deferral pattern idea
FromSent OnAttachments
Jonathan LaCour31 Mar 2008 06:40 
Michael Bayer31 Mar 2008 11:44 
Jonathan LaCour31 Mar 2008 12:05 
Subject:[sqlalchemy] Re: deferral pattern idea
From:Michael Bayer (mike@zzzcomputing.com)
Date:03/31/2008 11:44:24 AM
List:com.googlegroups.sqlalchemy

On Mar 31, 2008, at 9:41 AM, Jonathan LaCour wrote:

I've had a few situations recently when I've been optimizing some queries that I have been making through the ORM (via Elixir), and I've wanted to take advantage of deferred columns. However, I've found that defining which columns get deferred is often more appropriate to do at *query* time, not at the time of table/mapper definition.

I know about deferring groups of columns, and the "defer" option, but I'd really wish I could do something like this:

from sqlalchemy.orm import load_only

results = MyMappedClass.query.options( load_only('column_one', 'column_two', 'column_three') ).all()

I'll give you the private way to do it if you'd like to play with it:

results = query._select_context_options(only_load_props=['one', 'two', 'three']).all()

note that _select_context_options is not actually cloning the Query in this case.

Rather than what I am having to do in many cases where I have 20+ columns on a mapped object:

desired_columns = ['column_one', 'column_two'] query = MyMappedClass.query for column in MyMappedClass.table.c: if column.name not in desired_columns: query.options(defer(column))

Does this seem desirable to anyone else, or am I just crazy :)

since the query already can do this, it seems harmless enough to create a load_only() MapperOption. However, it seems in a way to be fundamentally different than defer(). With defer you can say defer('foo.bar.data') to defer the loading of some column multiple levels along the relation() chain. It makes less sense with a load_only() option, unless we said something like load_only([x, y, z], path="foo.bar") which seems entirely weird (or maybe not). So load_only() might be made to just apply to the "mapper zero" position in the Query to start with (im sure that's all you or most people would need it for anyway).