SQLAlchemy's "scale" Argument & WTForms
Note: Unless otherwise stated, this applies to SQLAlchemy 1.0.
Here are a few things I learned while researching the fix for this Flask-Admin Issue #1141:
- With SQLAlchemy’s Generic Float type, the “scale” argument is ignored. “scale” is not listed as an argument and the docs say “Additional arguments here are ignored by the default Float type.”. However, the object does have a “scale”, but it’s always “None”.
- WTForms’ “places” default for DecimalField’s is 2.
- The MySQL float does have a “scale” argument, it defaults to “None”. MySQL and Postgres have default limits on the length of the precision of floats, MySQL only shows 6 digits after the decimal and Postgres has a default column length of 17.
- Numeric columns do have a “scale” argument, and the default is “None” in SQLAlchemy 0.7 and 1.0.
decimal_return_scalewas added to both Float and Numeric in SQLAlchemy 0.9.- If you raise the number of “places” in WTForms’ DecimalField greater than
decimal_return_scalein the SQLAlchemy field, the digits after the decimal place set indecimal_return_scalewill show 0’s. - In SQLAlchemy,
_default_decimal_return_scale = 10is only used in a property/method called_effective_decimal_return_scale. The default fordecimal_return_scaleis “None” and not 10. Since_default_decimal_return_scaleand_effective_decimal_return_scalestart with an underscore, so it’s only intended for internal use. - MySQL’s “FLOAT” in SQLAlchemy doesn’t have
decimal_return_scale. It probably should? REAL and DOUBLE have this. - WTForms’ FloatField does not have “places”.
- With SQLAlchemy 1.0,
db.DECIMAL()will create a numeric(28, 6) in Postgres, but it creates a DECIMAL(10,0) in MySQL. This seemed pretty odd, since the default “scale” is “None”. Maybe it’s just using whatever the default is in each backend.
Sources:
- SQLAlchemy 0.7 docs for Float type: http://docs.sqlalchemy.org/en/rel_0_7/core/types.html?highlight=float#sqlalchemy.types.Float
- Latest SQLAlchemy docs for Float type: http://docs.sqlalchemy.org/en/latest/core/type_basics.html#sqlalchemy.types.Float
- Latest docs for MySQL Float type: http://docs.sqlalchemy.org/en/latest/dialects/mysql.html?highlight=float#sqlalchemy.dialects.mysql.FLOAT