I want to create field Datetime which will update current date time when a row updated. I tried this:
updated_on = Column(DateTime, onupdate=db.func.now())
and this:
updated_on = Column(DateTime, server_onupdate=db.func.now())
But a field is update only when I add a new row and no changes after update this row. upd:
class UserLog(db.Model):
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey('user.id'))
state = Column(String(25))
created_on = Column(DateTime, server_default=db.func.now())
updated_on = Column(DateTime, onupdate=datetime.utcnow)
First of all, server_onupdate does not do anything server-side, and it's only there so that SQLAlchemy knows the server is 'supposed to' generate a value upon update. It's really misleading. You have to manually configure your database to generate the value upon update. SQLAlchemy doesn't do it for you.
So use onupdate. onupdate=datetime.utcnow. Don't forget to import datetime.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With