if i got it right, then ... why are you using the session.query()
instead of select? 

afaik: 

- full objects: you can retrieve them back
if you query for them with from_self; 

- object ids: if you know them,
select for them only; else, inspect your object and look for primary
keys to select; 

- count of objects ids: wrap func.count on the column
above. 

perhaps: 

cols = [] 

if ids: 

 if count_ids: 


cols.append(func.count(obj.id)) 

 else: 

 cols.append(obj.id) #
assuming you know the column name; or 


cols.append(obj.c.get(obj.c.keys()[0])) # assuming the first column is
always the id -- i use another method for this, if you're interested


else: 

 cols.append(obj) # this wouldn't require the from_obj key in
select, but i'm just writing to my webmail .. lol 

if count_ids: #
let's make it simple 

 result = session.execute(select(cols,
from_obj=obj).limit(limit).offset(offset)) 

i don't know if this helps,
but using select as internals in my app is always a better and flexible
way to generate queries ... 

best regards, 

richard. 

Em 2014-03-30
15:34, Jonathan Vanasco escreveu: 

> No. I'm want to edit the target of
the 'select' from an existing query. 
> For many select operations, I
need three variations 
> - the full Objects 
> - just the ObjectIds 
> -
a count of the objects ( which is sometimes faster as an explicit
"select obj.id" than a "select obj" , so i can influence the pg query
planner more ) 
> At least 95% of our application hits an "internal api"
that generates the SqlAlchemy queries, instead of using SqlAlchemy
directly. This just gives us a more standard interface as we change
filtering defaults or make underlying db changes. 
> 
> Here's an
example of what I have occurring more than a few dozen times: 
> 
> def
_core_Foos( dbSession, kw_a=True, kw_b=False, kw_c=True, ids_only=false,
for_count=False): 
> if ids_only or for_count: 
> query =
dbSession.query( Foo.id ) 
> else: 
> query = dbSession.query( Foo ) 
>
### 
> ### lots of complex filtering and joining based on kwags here 
>
### dozens and dozens of lines 
> ### 
> if for_count : 
> return query

> query = query 
> .order_by( Foo.id.desc() ) 
> .limit( limit ) 
>
.offset( offset ) 
> return query 
> def get_Foos( dbSession, **kwargs
): 
> query = _core_Foos( dbSession, ids_only = True, **kwargs ) 
>
return query.all() 
> def get_fooIds( dbSession, **kwargs ): 
> query =
_core_Foos( dbSession, ids_only = True, **kwargs ) 
> return [ i[0] for
i in query.all() ] 
> def get_count_fooIds( dbSession, **kwargs ): 
>
query = _core_Foos( dbSession, for_count = True, **kwargs ) 
> return
query.count() 
> This works really well. I just don't really like that I
start the query off by selecting an id or object(ie, all columns). 
>
I'm trying to figure out if there are better ways to handle this. 
> 
>
-- 
> 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] [1].
> To post to this group,
send email to [email protected] [2].
> Visit this group at
http://groups.google.com/group/sqlalchemy [3].
> For more options, visit
https://groups.google.com/d/optout [4].
 

Links:
------
[1]
mailto:[email protected]
[2]
mailto:[email protected]
[3]
http://groups.google.com/group/sqlalchemy
[4]
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.

Reply via email to