Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask SQLAlchemy updated_on

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)
like image 925
cruim Avatar asked Jan 23 '26 09:01

cruim


1 Answers

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.

like image 107
Hurried-Helpful Avatar answered Jan 25 '26 23:01

Hurried-Helpful



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!