High numerical precision floats with MySQL and the SQLAlchemy ORM
floating-accuracy, mysql, orm, python, sqlalchemy
Solution
Per our discussion in the comments: `sa.types.Float(precision=[precision here])` instead of `sa.Float` allows you to specify precision; however, `sa.Float(Precision=32)` has no effect. See the documentation for more information.
Problem
I store some numbers in a MySQL using the ORM of SQLAlchemy. When I fetch them afterward, they are truncated such that only 6 significant digits are conserved, thus losing a lot of precision on my float numbers. I suppose there is an easy way to fix this but I can't find how. For example, the following code: ``` import sqlalchemy as sa from sqlalchemy.pool import QueuePool import sqlalchemy.ext.declarative as sad Base = sad.declarative_base() Session = sa.orm.scoped_session(sa.orm.sessionmaker()) class Test(Base): __tablename__ = "test" __table_args__ = {'mysql_engine':'InnoDB'} no = sa.Column(sa.Integer, primary_key=True) x = sa.Column(sa.Float) a = 43210.123456789 b = 43210.0 print a, b, a - b dbEngine = sa.create_engine("mysql://chore:BlockWork33!@localhost", poolclass=QueuePool, pool_size=20, pool_timeout=180) Session.configure(bind=dbEngine) session = Session() dbEngine.execute("CREATE DATABASE IF NOT EXISTS test") dbEngine.execute("USE test") Base.metadata.create_all(dbEngine) try: session.add_all([Test(x=a), Test(x=b)]) session.commit() except: session.rollback() raise [(a,), (b,)] = session.query(Test.x).all() print a, b, a - b ``` produces ``` 43210.1234568 43210.0 0.123456788999 43210.1 43210.0 0.0999999999985 ``` and I would need a solution for it to produce ``` 43210.1234568 43210.0 0.123456788999 43210.1234568 43210.0 0.123456788999 ```