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))
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:
Hope it helps!
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With