sqlalchmy automatically updates the time when inserting data

Method one, the code is automatically inserted into the database after the time is generated

class Task(BaseModel):
    __tablename__ = 'task_spiders'  # 表名 nullable
    # 定义各字段
    task_num = db.Column(db.Integer, autoincrement=True, primary_key=True, nullable=False, comment='任务ID/序号 自增 任务唯一标识')
    task_name = db.Column(db.String(255), nullable=False, comment='任务名称')

    create_time = db.Column(db.DateTime, default=datetime.datetime.now, comment='任务的创建时间')  # 记录的创建时间

Method 2: When the database inserts a field, the database adds time

from sqlalchemy import func

class Task(BaseModel):
    __tablename__ = 'task_spiders'  # 表名 nullable
    # 定义各字段
    task_num = db.Column(db.Integer, autoincrement=True, primary_key=True, nullable=False, comment='任务ID/序号 自增 任务唯一标识')
    task_name = db.Column(db.String(255), nullable=False, comment='任务名称')

    create_time = db.Column(db.DateTime, server_default=func.now(), comment='任务的创建时间')  # 记录的创建时间

Guess you like

Origin blog.csdn.net/weixin_41822224/article/details/109462211