[英]flask-sqlalchemy: data is not updated
I am running SQLAlchemy with MySQL in combination with a Flask API. 我正在结合MySQL和Flask API运行SQLAlchemy。 We use models for the requests and create and update changes with db.session.add() and db.session.commit().
我们使用模型来处理请求,并使用db.session.add()和db.session.commit()创建和更新更改。
We are also running the processing of the data in parallel - its an infinite while loop which queries the database on every run (Model.query.filter_by(...).all()) - however, we only get the data that was in the database before and not the updated data - the data itself is already in the database. 我们还并行运行数据处理-一个无限的while循环,每次运行时都会查询数据库(Model.query.filter_by(...)。all())-但是,我们仅获得在数据库中而不是更新数据之前-数据本身已经在数据库中。 If we restart the application, the new data is found.
如果我们重新启动应用程序,则会找到新数据。
The DB is created with db = SQLAlchemy(app) and then imported and used in the models 使用db = SQLAlchemy(app)创建数据库,然后将其导入并在模型中使用
class Model(db.Model):
name = db.Column(db.String(255), index=True, unique=True)
active = db.Column(db.Boolean)
def create(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def run_update():
while True:
models = Model.query.filter_by(active=True).all()
for model in models:
do_something(model)
time.sleep(300)
def do_something():
longtask = threading.Thread(target=long_task)
longtask.start()
def long_task():
// Update database here again
// Task has finished before it is run again.
time.sleep(10)
If a new entry is created, the result is only captured in the model query after restarting the app. 如果创建了新条目,则仅在重新启动应用程序后才在模型查询中捕获结果。
I already tried to replace the Model.query queries with db.session.query(Model) but that did not change the result. 我已经尝试用db.session.query(Model)替换Model.query查询,但是并没有改变结果。
Thanks in advance, 提前致谢,
我在while循环的末尾添加了一个db.session.close()-现在重新加载了数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.