User_id = Column(Integer, nullable=False)ĭef process_bind_param(self, value, dialect):ĭef process_result_value(self, value, dialect): Score = Column(Integer, server_default="0", nullable=False) Print "sqlalchemy._version_:", sqlalchemy._version_ Would you change sqlalchemys Float implementation to force the conversion to float in def process_result_value(.) or is there some other better way to achieve this? Or perhaps a different operator than cast should/could be chosen? Right now, the current behavior certainly surprised me. I think there is a good argument that the type Float(asdecimal=False) would behave more consistently if it would would allow to convert a column from a query via the cast operator as well as the column from a model. Is it the intentional and expected behavior for a cast to float for the result of a column in a query that it is just discarded? If so, how do I annotate a query to tell sqlalchemy that I would please like to get the result in a specific type? 22:44:36,067 INFO SELECT avg(coalesce(track.score, %s)) AS average_game_score To my understanding because Float implies Float(asdecimal=false).Īs a workaround we added this custom type: In normal columns I can tell SQLAlchemy that we want the type to be Float, even though the underlying type might be NUMERIC, and still sqlalchemy will convert the result to float for us. But more importantly, there seems to be no better way to express what format the returned column should have. This shows that the cast is actually discarded in sql (which makes some sense as mysql can't cast to float, but only to decimal - which doesn't make sense, but seems to be the case). 22:44:36,062 INFO SELECT avg(coalesce(track.score, %s)) AS average_game_score 22:44:36,059 INFO SELECT track.score AS track_score, erid AS track_userid 22:44:36,057 INFO SELECT CAST('test collated returns' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin AS anon_1 22:44:36,056 INFO SELECT CAST('test unicode returns' AS CHAR(60)) AS anon_1 22:44:36,055 INFO SELECT CAST('test plain returns' AS CHAR(60)) AS anon_1 22:44:36,053 INFO show collation where `Charset` = 'utf8' and `Collation` = 'utf8_bin' 22:44:36,051 INFO SHOW VARIABLES LIKE 'sql_mode' Print session.query(cast(func.avg(alesce(Track.score, 0)), Float).label('average_game_score')).group_by(er_id).all() Print session.query(Track.score, er_id).limit(20).all() # print session.query(cast(func.avg(alesce(Track.score, 0)), Float).label('average_game_score')) User_id = Column('userid', Integer, nullable=False)įrom import castįrom sqlalchemy.types import Integer, Float Score = Column('score', Integer, server_default="0", nullable=False) Id = Column('idtrack', Integer, primary_key=True) Print "sqlalchemy._version_", sqlalchemy._version_įrom import declarative_baseįrom sqlalchemy import Column, Integer, String
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |