Michael Bayer ha scritto:
> 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()
>
>
The add_columns is a good option , but using the aggregate function
"func.count" is useless because aggregating columns from a Mapper must
contain every column all time.
Example:
create table people (
name text,
surname text,
type CASE 'A','B','C'
)
There is no solution to do for example the simple query based over the
mapper People:
select count(type) from people group by type;
only solution is to use the select functioon.
Glauco
--
+------------------------------------------------------------+
Glauco Uri - Programmatore
glauco(at)allevatori.com
Sfera Carta Software® [EMAIL PROTECTED]
Via Bazzanese,69 Casalecchio di Reno(BO) - Tel. 051591054
+------------------------------------------------------------+
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---