简体   繁体   English

flask_sqlalchemy UPDATE 行详细信息不更新 mySQL 数据库

[英]flask_sqlalchemy UPDATE row details not updating mySQL database

I have a flask app and a mySQL database connected to it.我有一个 Flask 应用程序和一个连接到它的 mySQL 数据库。 I am using POSTMAN to send GET/POST requests to the flask endpoints for user add, update and delete.我正在使用 POSTMAN 将 GET/POST 请求发送到烧瓶端点以进行用户添加、更新和删除。 I am able to successfully add users to the database but while I am updating the flask app does not throw any errors where as the postman also displays a successful JSON response that "User updated!!!"我能够成功地将用户添加到数据库中,但是在我更新 Flask 应用程序时不会抛出任何错误,因为邮递员还显示了一个成功的 JSON 响应,即"User updated!!!" But when I perform SELECT * FROM Users operation in my SQL Workbench the details are not being updated.但是当我在我的 SQL Workbench 中执行SELECT * FROM Users操作时,细节没有被更新。

My implementation is as follows - I have created a model User with two columns username and password .我的实现如下 - 我创建了一个带有两列usernamepassword的模型User

The user update API looks like -用户更新 API 看起来像 -

from flask_restful import Resource
from flask_sqlalchemy import SQLAlchemy
from model import User


class Update_User(Resource):
    def post(self):
        data = request.get_json(force=True)
        usr = User.query.filter_by(username=data['username']).first()
        print(1, usr.username, usr.password)
        
        usr.password = data['new_password']
        print(2, usr.username, usr.password)

        db.session.commit()
        db.session.close()

        return {"res": "User updated!!!!"}

flask output:烧瓶输出:

1 Ash 12345
2 Ash 00000

Just for better understanding, what is better to use ?只是为了更好地理解,什么更好用? Python SQLAlchemy or flask's own SQLAlchemy ? Python SQLAlchemy 还是 Flask 自己的 SQLAlchemy ? Because there are other instances as well where I am facing trouble like while updating two rows simultaneously using add_all()因为在其他情况下我也遇到了麻烦,比如使用add_all()同时更新两行

After struggling I found out where I was wrong, someone else facing the issue can try the following thing.在挣扎之后,我发现了我错在哪里,遇到问题的其他人可以尝试以下操作。

What worked for me -什么对我有用 -

usr.password = data['new_password']
db.session.merge(usr)
db.session.commit()

Things that did not work for me despite having them as answer on some of the stackoverflow questions -尽管将它们作为某些 stackoverflow 问题的答案,但对我不起作用的事情 -

usr.data["password"] = data['new_password']
db.session.commit()
--------------------------------------------
usr.password = data['new_password']
db.session.add(usr)
db.session.commit()
--------------------------------------------
usr.data = { "password": data['new_password'] }
db.session.commit()

Hope this might help you!希望这可以帮助你!

You need to add the object to the session to tell SQLAlchemy that you want to you've made changes (after updating it, before committing):您需要将对象添加到会话中,以告诉 SQLAlchemy 您希望您已经进行了更改(更新之后,提交之前):

usr.password = data['new_password']
db.session.add(usr)
db.session.commit()

See more details in the SQLAlchemy docs: https://docs.sqlalchemy.org/en/13/orm/tutorial.html#adding-and-updating-objects在 SQLAlchemy 文档中查看更多详细信息: https : //docs.sqlalchemy.org/en/13/orm/tutorial.html#adding-and-updating-objects

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

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