Take the 2-minute tour ×

I know I can lazy load columns from a table:

      for colname in ['col4', 'col5', ..,  'colN'])

I only want the first 3 columns, so can shorten that to the following:

      for col.name in MyTable.__table__.columns
      if col.name not in ['col1', 'col2', 'col3'])

Because MyTable has a few extra column_propertys which aren't listed in MyTable.__table__.columns, these get included in the query even though I don't want them.

So is there a construct to express what I want directly as a restriction? e.g. something like

session.query(MyTable).options(XXselect('col1', 'col2', 'col3'))
share|improve this question
There is this suggestion which uses defer_everything_but() in this similar thread: stackoverflow.com/questions/6977658/…. I stumbled on this question because I wanted to follow a relationship, which requires the full object. But I didn't want to select all the columns (for example, if the user doesn't have read privileges on all the columns). I don't know of any other way to follow a relationship from just the keyedtuples, –  Kevin Dec 9 '13 at 17:17
add comment

1 Answer

If you only want the values, you can just call :

session.query(Mytable.col1, MyTable.col2, MyTable.col3).

If you want the full object with deferred columns, MyTable.__table__.columns should give you all the columns regardless of the mixins.

share|improve this answer
Yes, do want the full object –  EoghanM Jul 10 '12 at 11:50
Apologies, you are correct in saying that MyTable.__table__.columns includes mixins ..... in fact the extra columns are appearing in the query because of a column_property. I've updated my answer to reflect this. –  EoghanM Jul 10 '12 at 11:57
I'm sorry, I think I won't be able to help you. The only other idea I have is using deferred on each column, but that doesn't sound like a good idea. Also, maybe adding the your table definition with the column_property might help (from what I have seen, there are different ways to use it). –  madjar Jul 10 '12 at 12:27
add comment

Your Answer


