SQLAlchemy: how to implement float splitting on update?

I have an update request like this:

DBSession.query(Model).filter(Model.id == id).update({
    Model.count:  Model.count + 1,
    Model.wins:   Model.wins + trigger,
    Model.rating: (Model.wins + trigger) / (Model.count + 1)
}, synchronize_session=False)

      

When I use this it always returns Models.rating = 0

because wins < count

. But I need to get a floating point number for example 0.02389

.

How do I get it? Maybe there is some split function in sqlalchemy (which I didn't find) or smth else ...

Thank!

+3


source to share


2 answers


This question is old, but I didn't find an answer in other threads, casting worked for me :



from sqlalchemy import cast, Float

cast(Model.wins, Float) / cast(Model.count, Float)

      

+1


source


I think all you have to do is force python to do float splitting:

float(Model.wins + trigger) / float(Model.count + 1)

      



From your code, it looks like it has nothing to do with sqlalchemy, but it is just a regular expression that is evaluated when initializing the dictionary that is passed to update

.

This will bite me all the time too ...

0


source







All Articles