Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqlalchemy mysql FLOAT precision and length

I'm trying to model an existing database with SQLAlchemy and I am very close.

My sticking point is the FLOAT types.

In the MySQL database they are Float(10,2)

Using SQLAlchemy, I'm unable to replicate this regardless of how I try to create them.

Examples are:

from sqlalchemy.dialects.mysql import FLOAT
from sqlalchemy.ext.declarative import declarative_base

connection_string = 'xxxxx'

engine = create_engine(connection_string, echo=True, encoding='utf-8')
Base = declarative_base(engine)

class TestTable(Base):
    __tablename__ = 'my_test_table'
    float_1 = Column(FLOAT(length=10, precision=2))
    float_2 = Column(Float(10,2))
    float_3 = Column(Float(as_decimal=True))
like image 655
Alex Hellier Avatar asked Feb 09 '18 12:02

Alex Hellier


2 Answers

If you want to get adaptative scale, instead of fixed, I would suggest to use as follows:

lat = Column(FLOAT(precision=10, decimal_return_scale=None))

More specifically, let's consider following lat float: 45.7421773

Here are parameters possibility:

lat_precision_adaptative_scale = Column(FLOAT(precision=32, decimal_return_scale=None))
lat_precision_fixed_scale = Column(FLOAT(precision=32, scale=10))
lat_no_precision_fixed_scale = Column(FLOAT(scale=3))
lat = Column(FLOAT)

It will insert following results: sql_alchemy_precision_scale_return_scale_parameter

Hope it helps!

like image 67
sashaboulouds Avatar answered Sep 26 '22 05:09

sashaboulouds


I think I'm right in saying that you're trying to set precision and scale? Have you tried:

float = Column(FLOAT(precision=10, scale=2))

Your definitions of float_2 and float_3 don't appear to be using the imported MySQL dialect FLOAT class, and may be using another class here which may be adding to the confusion!

like image 33
Sean Avatar answered Sep 27 '22 05:09

Sean