简体   繁体   中英

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.

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. 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. I want to make this behaviour specific to the database (just like triggers and timers in SQL). How to make it in sql-alchemy?

you could insert an additional column, in which you write for which date allowed_access_per_day was set. In the query you check then, if it is from the previous day.

In core sqlalchemy I haven't found something like jobs/scheduling. There are some additonal packages as addon to sqlalchemy. It seems that you have to write the scheduling of

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

yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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