On Apr 24, 2007, at 4:05 PM, Chris Shenton wrote:

> Am I being stupid about not seeing the difference -- what keywords and
> arguments I can use -- between:
>
>   self.session.query(MyClass).select(...)
>
> and
>
>   select(...)
>
>

these two methods are fundamentally different.  first of all,  
session.query(MyClass) deals with object relational mapping, and  
select() deals with result sets...i think you got that part.

so the big thing with query(MyClass).select() is that its designed to  
return object instances.  in order to return object instances, the  
mapper already knows what columns it wants to select...so in the vast  
majority of usages with query(), you dont specify any "columns"  
clause, and the "columns" argument, which you know from  
sqlalchemy.sql.select(),  is not part of the select() method on  
sqlalchemy.orm.query.Query.

so the basic answer to your question is, sqlalchemy.sql.select()  
takes the list of columns as its first argument, query(MyClass).select 
() does not.  in the latter case, the "columns" clause is  
automatically all of the columns from MyClass' table.  there is a  
"group_by" keyword argument available on query.select(), as well as a  
generative method group_by() which is used like query.group_by(<list  
of columns>).select()

however, Query() has a whole boatload of new features that came out  
in 0.3.6.  the best way to get a sense for everything that query can  
do is to look at the generatted docs, which i now have linked from  
the main docs in two places (ive also recently rewritten the whole  
documentation on Query, as of last weekend, worth checking out):

http://www.sqlalchemy.org/docs/ 
sqlalchemy_orm_query.html#docstrings_sqlalchemy.orm.query_Query

as it turns out, theres a neat way to do exactly what you want with  
this most recent Query object (the goal being, return object  
instances plus an aggregate value in one shot).  do it like:

session.query(Fault).add_column(func.count(Fault.c.severity).label 
('count')).group_by([c for c in Fault.c]).filter(Fault.c.ts_created  
 >= self.this_week).list()

where session.query(Fault) will in all cases use all of the columns  
from Fault's table in a SELECT statement, and add_column() adds an  
extra column along.  because you added an external column, the  
results will be expressed as tuples consisting of the entity and the  
numerical count.

notice also that the word "select()" isnt even in there.  the newer  
style establishes itself as more clearly distinct from the base SQL  
construction system.

the GROUP BY clause there is needed to tell mysql about all the  
columns that are not part of your aggregate function (the aggregate  
being the "count" function)...since all the columns from Fault's  
table will be in the columns clause, you need to add them all to the  
group by.






--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to