On Jan 5, 2013, at 1:53 PM, Alexander Jacob wrote:

> 
> I preferred the event solution but
> 
> class Country(Base):
>     __tablename__ = 'country'
>     name = Column('name_<I18N>', String(64))
> 
> 'SELECT country.`name_<I18N>` AS `country_name_<I18N>`, country.id AS 
> country_id, country.iso2 AS country_iso2 
> FROM country 
> WHERE country.id = %s'
> 
> gets properly replace in before_cursor_execute to 
> 
> 'SELECT country.`name_en` AS `country_name_en`, country.id AS country_id, 
> country.iso2 AS country_iso2 
> FROM country 
> WHERE country.id = %s'

ah yeah.  there's a way to manipulate the column lookup as well here which is 
inside of the "context" passed to the event, I seem to recall working this out 
for someone but I'd have to re-think into a recipe here.   It starts using 
things that aren't 100% public, though.   or you could try a more elaborate 
regexp that skips the label names, or one that does a second replace for 
"tablename_colname_en" back to the original symbol since that's the labeling 
scheme.

but before I get into that....

> 
> This will work but this will load all columns from the db... 

because your country is a fixed global, you can actually just map the column 
here as needed:

class Country(Base):
   # ...

   name = Column('name_%s' % GLOBAL_COUNTRY, String)

then you don't even declare the other columns on this Table, how about that ?   
 there's also ways to have the columns on the Table, but not mapped, using 
exclude_columns.

otherwise, if GLOBAL_COUNTRY changes at runtime, deferred() might be an 
approach:

class Country(Base):
   # ...

   name_de = deferred(Column(String))
   name_en = deferred(Column(String))

name_de and name_en won't be part of queries, and only load when they are 
accessed.  but this emits a second SELECT.   You'd probably want some way to 
undefer the one you need at Query time, that looks like this:

query(Country).options(undefer(Country.name_en))

but you'd probably want to automate that somehow.


-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to