python - SQLAlchemy Turning Floats Into Strings in SQL? -
i have model looks this:
class property(base): __tablename__ = 'property' id = column('id', integer, primary_key=true, autoincrement='auto') name = column('name', string(255)) growth = column('growth', float(2))
in mysql, growth column of type float(6,2).
when query on name, match fine.
prop_obj = session.query(property).filter(property.name == fund_dict['name'])
if add float property, growth, filter() clause not able match in db.
prop_obj = session.query(property).filter(property.name == func_dict['name'], property.growth == func_dict['growth'])
per sqlalchemy docs, when print out sql statement, see this:
>>> statement = prop_obj.statement >>> print(statement.compile(dialect=mysql.dialect())) select `property`.`id`, `investment`.`name`, `property`.`growth` `property` `property`.`name` = %s , `property`.`growth` = %s
does %s indicate substitution that's occurring string , not float? explain why it's not matching. if so, how make substitution float instead of string?
i'm using python 3.5 , sqlalchemy 1.0.11. thanks.
no, %s
placeholder parameters. it %s
. not %-formatting parameters string. can see evidence of in query, literal being compared name
not quoted.
what might happening instead you're trying compare different floating point value in database. because floating point numbers not stored exactly, exact comparisons problematic. should doing instead see if difference between value , 1 in db less threshold:
func.abs(property.growth - func_dict['growth']) < 0.01
Comments
Post a Comment