SqlAlchemy won't accept datetime.datetime.now value in a DateTime column

前端 未结 4 1521
独厮守ぢ
独厮守ぢ 2021-02-18 23:59

I should first mention that I\'m using SqlAlchemy through Flask-SqlAlchemy. I don\'t believe this affects the issue but if it does, please let me know.

Here is the relev

相关标签:
4条回答
  • 2021-02-19 00:26

    Set the import out the class.

    from datetime import datetime
    
    last_updated = db.Column(db.DateTime, default=datetime.now())
    

    or

    import datetime
    
    last_updated = db.Column(db.DateTime, default=datetime.datetime.now())
    

    I did this

    0 讨论(0)
  • 2021-02-19 00:27

    Look at your line:

    last_updated = db.Column(db.DateTime, default=datetime.datetime.now)
    

    There is no datetime.datetime.now attribute in python. However, there is a datetime.datetime.now() function in python. You were just missing a pair of parentheses.

    0 讨论(0)
  • 2021-02-19 00:31
    last_updated = db.Column(db.DateTime, default=db.func.current_timestamp())
    

    I think this will work

    0 讨论(0)
  • 2021-02-19 00:40

    Try to use datetime.datetime.utcnow(). This works for me.

    0 讨论(0)
提交回复
热议问题