> On Nov 12, 2014, at 3:18 AM, Thomas Wanschik <twansc...@googlemail.com> wrote: > > 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.
the Float type doesn’t do any coercion on MySQL because the MySQL drivers typically return floats directly in the cases they’re supposed to. Use Numeric(usedecimal=False) instead. > > 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 <twan...@googlemail.com >> <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 sqlalchemy+...@googlegroups.com <javascript:>. >> To post to this group, send email to sqlal...@googlegroups.com <javascript:>. >> Visit this group at http://groups.google.com/group/sqlalchemy >> <http://groups.google.com/group/sqlalchemy>. >> For more options, visit https://groups.google.com/d/optout >> <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 sqlalchemy+unsubscr...@googlegroups.com > <mailto:sqlalchemy+unsubscr...@googlegroups.com>. > To post to this group, send email to sqlalchemy@googlegroups.com > <mailto:sqlalchemy@googlegroups.com>. > Visit this group at http://groups.google.com/group/sqlalchemy > <http://groups.google.com/group/sqlalchemy>. > For more options, visit https://groups.google.com/d/optout > <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 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.