简体   繁体   English

Sqlalchemy:如何每 24 小时更新一次列?

[英]Sqlalchemy: How to update a column every 24 hours?

I am working on A REST API with python flask and I have implemented the following schema via sql-alchemy.我正在使用 python flask 开发 REST API,我已经通过 sql-alchemy 实现了以下模式。

from app import db
from datetime import datetime
from app.utilities.commons import *

class User(db.model):
    id = id = db.Column(db.String(36), primary_key=True, unique=True, default=get_unique_id())
    username = db.Column(db.String(64), unique=True, nullable=False)
    first_name = db.Column(db.String(64), nullable=False, unique=False)
    last_name = db.Column(db.String(64), nullable=False, unique=False)
    organism = db.Column(db.String(35), nullable=False, unique=False)
    allowed_access_per_day = db.Column(db.Integer, nullable=False, default=0)
    last_modified = db.Column(db.DateTime, nullable=True, default=None)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

What I want to do is to reset the attribute allowed_access_per_day (update it to 0) everyday 00h00 am.我想要做的是每天上午 00 点重置属性allowed_access_per_day (将其更新为 0)。 What I am doing as a temporary solution is to make a python thread that update the table every 24h which is not a good idea as it does not keep the same behaviour to all application connecting to the database.作为临时解决方案,我正在做的是创建一个 python 线程,每 24 小时更新一次表,这不是一个好主意,因为它不会对所有连接到数据库的应用程序保持相同的行为。 I want to make this behaviour specific to the database (just like triggers and timers in SQL).我想让这种行为特定于数据库(就像 SQL 中的触发器和计时器一样)。 How to make it in sql-alchemy?如何在 sql-alchemy 中制作它?

you could insert an additional column, in which you write for which date allowed_access_per_day was set.您可以插入一个附加列,在其中写入设置了allowed_access_per_day的日期。 In the query you check then, if it is from the previous day.然后在查询中检查它是否来自前一天。

In core sqlalchemy I haven't found something like jobs/scheduling.在核心 sqlalchemy 中,我还没有找到类似工作/调度的东西。 There are some additonal packages as addon to sqlalchemy.有一些附加包作为 sqlalchemy 的插件。 It seems that you have to write the scheduling of看来你必须写调度

users.update().values(allowed_access_per_day =0)

yourself.你自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM