简体   繁体   English

Flask-sqlalchemy Rowproxy 无法修改

[英]Flask-sqlalchemy Rowproxy cannot be modified

I am trying to update records 'status' from my database where the 'sendtime' is in the past.我正在尝试从我的数据库中更新“发送时间”过去的记录“状态”。 I am receiving a set of records that I can iterate over and see all their fields with no issue.我收到了一组记录,我可以遍历这些记录并毫无问题地查看它们的所有字段。 The issue arises when I attempt to update the status field.当我尝试更新状态字段时出现问题。

The following results in Rowproxy does not support item assignment : Rowproxy 中的以下结果不支持项目分配

query = ('SELECT * FROM jobs '
    'WHERE status="INCOMPLETE" '
    'AND sendtime <= NOW()')
overdueJobs = db.engine.execute(query).fetchall()
for overdueJob in overdueJobs:
    overdueJob.status = "COMPLETE"
db.session.commit()

I read this answer on SO based on the error above and while converting to a dict allows me to edit the fields without errors, it does not actually commit anything to the database.我根据上面的错误在 SO上阅读了这个答案,虽然转换为 dict 允许我在没有错误的情况下编辑字段,但它实际上并没有向数据库提交任何内容。

I believe I am doing something wrong regarding how I use the queried results.我相信我在如何使用查询结果方面做错了。 I think I have to use my jobs model somehow to access the fields, but the documentation and other questions I found on SO are for a single result from a query.我想我必须以某种方式使用我的工作模型来访问这些字段,但是我在 SO 上找到的文档和其他问题是针对查询的单个结果的。 What I have can be more than that.我所拥有的可能不止这些。 How do I edit the fields of each record returned from this query?如何编辑从此查询返回的每条记录的字段?

I found the answer to my question here .我在这里找到了我的问题的答案。

I needed to modify my query from我需要修改我的查询

overdueJobs = db.engine.execute(query).fetchall()

To

overdueJobs = jobs.query.from_statement(db.text(query)).all()

Where jobs is a model in my database.其中, jobs是我数据库中的一个模型。 Then I could access each row as a model by looping over the returned result.然后我可以通过循环返回的结果来访问每一行作为模型。

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

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