如何使用 SQLAlchemy 和会话进行更新?

How to update using SQLAlchemy and session?

当我想在我的产品中添加一行时 table 我这样做:

p = models.MyProducts(code="123456")
db.session.add(p)
db.session.commit()

里面models.py我有:

class MyProducts(db.Model):
    id = db.Column(db.Integer, primary_key = True, autoincrement=True)
    code = db.Column(db.Integer)
    quantity = db.Column(db.Integer, default=1)
    comment = db.Column(db.String(99999), default="")
    pricepaid = db.Column(db.Float(264), default="0.0")

如何更新代码为“123456”的行? 我试过了

updater = models.MyProducts.query.filter_by(code="123456").update({'comment':"greeeeat product"})
db.session.add(updater)
db.session.commit()

但这行不通。

假设只有一个产品:

my_product = models.MyProducts.query.filter_by(code=123456).first()  # @note: code is Integer, not a String, right?
if my_product:
    my_product.comment = "greeeeat product"
    db.session.add(my_product)
    db.session.commit()