When using "echo='debug'" I get the following:

In [22]: session.query(func.sum(Event.cycle)).scalar()
2014-11-12 08:14:35,697 INFO sqlalchemy.engine.base.Engine SELECT 
sum(metrics_event.cycle) AS sum_1
FROM metrics_event
SELECT sum(metrics_event.cycle) AS sum_1
FROM metrics_event
2014-11-12 08:14:35,697 INFO sqlalchemy.engine.base.Engine ()
()
2014-11-12 08:14:35,698 DEBUG sqlalchemy.engine.base.Engine Col ('sum_1',)
Col ('sum_1',)
2014-11-12 08:14:35,698 DEBUG sqlalchemy.engine.base.Engine Row 
(Decimal('50'),)
Row (Decimal('50'),)
Out[22]: Decimal('50')

It seems like mysql returns a Decimal object. 

Moreover, I thought that I can use

session.query(type_coerce(func.sum(Event.amount / Event.cycle), 
Float)).scalar()

for more complex aggregates but this doesn't work. Seems like I have to 
manually convert values in python.

Thanks for your help.

On Tuesday, November 11, 2014 7:18:13 PM UTC+1, Michael Bayer wrote:
>
> The MySQL console can’t show you this because “Decimal” is a Python type. 
>   the way to see what is going on is to test with the MySQLdb driver 
> directly, though in this case it’s easy enough to see it also with 
> “echo=‘debug’”.
>
>
> On Nov 11, 2014, at 1:09 PM, Thomas Wanschik <[email protected] 
> <javascript:>> wrote:
>
> Thanks for your answer.
>
> I did the exact same raw sql query (got the query string via echo=True) 
> and it seems like mysql returned an integer (console print looked like a 
> integer). So I concluded it has to be converted somewhere on the way 
> through sqlalchemy.
>
>
> On Tuesday, November 11, 2014 6:25:44 PM UTC+1, Thomas Wanschik wrote:
>>
>> Hi,
>> the query
>>
>> session.query(func.sum(MyModel.amount)).scalar()
>>
>> with 
>>
>> MyModel.amount = Column(SmallInteger, nullable=False)
>>
>> returns a Decimal instead of an integer when using mysql with the default 
>> driver (as well as with the cymysql driver).
>>
>> How can I get this query to return an integer? And why can't it return an 
>> integer automatically?
>>
>> Thanks in advance
>>
>
> -- 
> 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] <javascript:>.
> To post to this group, send email to [email protected] 
> <javascript:>.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit 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