Amazing! Thank you!

On 11/20/2013 04:44 PM, Michael Bayer wrote:
the short answer is that DOUBLE is doing a Decimal conversion here and that’s 
where the digits are being lost.

So fix the problem immediately using asdecimal=False, then also make sure you 
use repr() to print out your type as str() for Python floats also truncates:

class SomeClass(Base):
    __tablename__ = 'someclass'
    id = Column(Integer, primary_key=True)
    value = Column(DOUBLE(asdecimal=False))

x = s.query(SomeClass).get(1)
print repr(x.value)

longer answer, I was a bit surprised that DOUBLE has asdecimal=True by default, 
but OK, but in any case I was surprised to see that SQLAlchemy’s Float type 
hardcodes the number of digits to 10 when it converts from float to Decimal, 
this will be fixed in 0.9 using the patch up at 
http://www.sqlalchemy.org/trac/attachment/ticket/2867/.

If you do want Decimal objects back in this case with DOUBLE, I’d subclass 
mysql.DOUBLE and specify a new result_processor() method.





On Nov 20, 2013, at 7:00 AM, Sebastian Elsner <sebast...@risefx.com> wrote:

Hello,

I am inserting float data into a MySQL column of type DOUBLE. The data gets 
inserted properly (verified via mysql terminal). Querying for the object 
returns a Decimal object, but  the number of digits after the decimal point if 
always limited to 11 (while DOUBLE supports 15 by default). So, what can I do 
to get the correct value? I have played with the precision and scale arguments 
for DOUBLE without effect.

Here's an example:


from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.engine import create_engine
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.schema import Column
from sqlalchemy.types import Integer

from sqlalchemy.dialects.mysql.base import DOUBLE

Base = declarative_base()


class SomeClass(Base):
    __tablename__ = 'someclass'
    id = Column(Integer, primary_key=True)
    value = Column(DOUBLE)

engine = create_engine('mysql://user:pass@localhost/test', echo=True)
Base.metadata.create_all(engine)
s = sessionmaker(engine)()
r = SomeClass(value=0.1234567891234567)
s.add(r)
s.commit()
x = s.query(SomeClass).get(1)
print x.value

Regards

Sebastian

--
check out www.pointcloud9.com

Sebastian Elsner - Pipeline Technical Director - RISE

t: +49 30 20180300 flor...@risefx.com
f: +49 30 61651074 www.risefx.com

RISE FX GmbH
Schlesische Strasse 28, Aufgang B, 10997 Berlin
c/o action concept, An der Hasenkaule 1-7, 50354 Hürth
Geschaeftsfuehrer: Sven Pannicke, Robert Pinnow
Handelsregister Berlin HRB 106667 B

--
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/groups/opt_out.


--
check out www.pointcloud9.com

Sebastian Elsner - Pipeline Technical Director - RISE

t: +49 30 20180300 flor...@risefx.com
f: +49 30 61651074 www.risefx.com

RISE FX GmbH
Schlesische Strasse 28, Aufgang B, 10997 Berlin
c/o action concept, An der Hasenkaule 1-7, 50354 Hürth
Geschaeftsfuehrer: Sven Pannicke, Robert Pinnow
Handelsregister Berlin HRB 106667 B

--
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/groups/opt_out.

Reply via email to