On Mar 27, 2014, at 10:08 AM, Josh Kuhn <deontologic...@gmail.com> wrote:

> I have a situation where I need to produce a select object, and then later, 
> one of the fields needs to be "zeroed" out conditionally.
> 
> so something like:
> 
> def select_ab(param):
>     from_obj = join(A, B, A.c.b == B.c.b)
>     return select([A.c.a, B.c.b, B.c.d], from_obj=from_obj).where(A.c.a == 
> param)
> 
> ...
> 
> sql_query = select_ab(34)
> 
> # Would like to do something like this:
> if some_condition:
>    sql_query = sql_query.replace_column(B.c.d, literal(0))
> 
> engine.execute(sql_query)
> 
> 
> I tried to hack together a "replace_column" function using 
> Select.with_only_columns, but as the documentation indicates, I need the 
> original list of columns in order to really do what I want to do, I can't 
> take the select's columns. And in my case, the original select's columns are 
> trapped inside the function select_ab. I'd like to be able to replace a 
> column on any arbitrary select if the column exists in the select.
> 
> Is there a way to do what I'm trying to do? Or do I just need to keep the 
> original columns around?

the original columns are in the select() object.   They're first in their 
original form in a list called _raw_columns, that is, if you had 
select([sometable, somecol, someothercol]) it would be literally those three 
elements, and then there is a public accessor called inner_columns which is 
derived directly from _raw_columns, it just expands a selectable (e.g. 
something with a .c. collection) into individual columns.

So it should be feasible to say something equivalent to:  
select.with_only_columns([(literal(0) if c is my_special_thing else c) for c in 
select.inner_columns])


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

Reply via email to