Flask-Sqlalchemy:数据库查询没有 return 新数据
Flask-Sqlalchemy: DB queries don't return new data
我正在构建一个应用程序,它从一项服务接收 webhook,将数据存储在数据库中,然后通过 API 提供数据。
我能够成功地将数据添加到我的应用程序,但是当我查询数据库时,我只收到上次启动应用程序时数据库中的第一次提交。
例如,如果我在启动应用程序时在订单 table 中有 26 个订单,然后触发 webhook,Order.query.all()
将 return 27 个订单,直到我重新启动应用程序,无论 table 中实际有多少订单(我可以使用 MySQL 进行验证)。
这里是 class 的一个示例,用于将数据插入 table:
@webhook.route('/order/insert', methods=['POST'])
def insert_orders():
soda_json = request.json
db.session.add(Order(
order_id=soda_json['id'],
created_at=datetime.datetime.now(),
currency=soda_json['currency'],
total_line_items_price=soda_json['total_line_items_price'],
refunds=sum(float(i) for i in soda_json['refunds'] if soda_json['refunds']),
shipping_lines_price=sum([float(line['price']) for line in soda_json['shipping_lines']]),
note=soda_json['note']
))
db.session.commit()
return '200'
这是我用于测试的基本 API 方法:
order_test = Order.query.all()
@api.route('/display', methods=['POST', 'GET'])
def display_test():
return jsonify(json_list=[i.serialize for i in order_test]), '200'
总是获取最新数据我错过了什么?
据我所知,问题是 order_list 仅在启动该视图时才被填充。因此,如果您将该行代码移动到您的路由调用中,那么每次调用该路由时它都会被刷新。
例如
@api.route('/display', methods=['POST', 'GET'])
def display_test():
order_test = Order.query.all()
return jsonify(json_list=[i.serialize for i in order_test]), '200'
从你到目前为止所说的看来,无论新数据发送到 web 挂钩多少次,你似乎只能向数据库添加一条新记录,如果你重新启动 API 然后你回到原点,新记录不再存在。
对我来说,在 webhook 中提交事务似乎是一个问题,因为在调用 db.session.add() 之后,数据不会保存到db,因此事务保持打开状态,因此当添加新数据时,它可能会覆盖上一次调用的数据,然后当您结束 API 时,事务要么 committed 或 rollbacked(不记得 flask-alchemy 的默认操作)。您可能需要检查数据本身并查看调用 webhook 后第 51 行中正在 return编辑的数据,并查看在将新数据发送到 webhook 后它是否发生变化。
如果您还比较上面的 webhook 代码和下面的提交和 return 行是不同的。在你的中,它们位于不同的制表符行上,并且在 webhook 函数之外,并且在调用 webhook 时不会得到 运行,因此会有一个开放的事务。
@webhook.route('/order/insert', methods=['POST'])
def insert_orders():
soda_json = request.json
db.session.add(Order(
order_id=soda_json['id'],
created_at=datetime.datetime.now(),
currency=soda_json['currency'],
total_line_items_price=soda_json['total_line_items_price'],
refunds=sum(float(i) for i in soda_json['refunds'] if soda_json['refunds']),
shipping_lines_price=sum([float(line['price']) for line in soda_json['shipping_lines']]),
note=soda_json['note']
))
db.session.commit()
return '200'
查询中的方法顺序似乎有问题。
from my_app.models import Order
order_test = Order.query.all()
这是教程中的结构(https://pythonhosted.org/Flask-SQLAlchemy/queries.html#querying-records),但看起来可能只是查看原始导入模型中的数据。请随时纠正我。
在 flask shell 中的类似操作中,我已经成功地在使用此查询结构提交后立即获取实时数据:
db.session.query([model]).all()
因此 API 方法的一个工作示例可能是:
@api.route('/display', methods=['POST', 'GET'])
def display_test():
order_test = db.session.query(Order).all()
return jsonify(json_list=[i.serialize for i in order_test]), '200'
我正在构建一个应用程序,它从一项服务接收 webhook,将数据存储在数据库中,然后通过 API 提供数据。
我能够成功地将数据添加到我的应用程序,但是当我查询数据库时,我只收到上次启动应用程序时数据库中的第一次提交。
例如,如果我在启动应用程序时在订单 table 中有 26 个订单,然后触发 webhook,Order.query.all()
将 return 27 个订单,直到我重新启动应用程序,无论 table 中实际有多少订单(我可以使用 MySQL 进行验证)。
这里是 class 的一个示例,用于将数据插入 table:
@webhook.route('/order/insert', methods=['POST'])
def insert_orders():
soda_json = request.json
db.session.add(Order(
order_id=soda_json['id'],
created_at=datetime.datetime.now(),
currency=soda_json['currency'],
total_line_items_price=soda_json['total_line_items_price'],
refunds=sum(float(i) for i in soda_json['refunds'] if soda_json['refunds']),
shipping_lines_price=sum([float(line['price']) for line in soda_json['shipping_lines']]),
note=soda_json['note']
))
db.session.commit()
return '200'
这是我用于测试的基本 API 方法:
order_test = Order.query.all()
@api.route('/display', methods=['POST', 'GET'])
def display_test():
return jsonify(json_list=[i.serialize for i in order_test]), '200'
总是获取最新数据我错过了什么?
据我所知,问题是 order_list 仅在启动该视图时才被填充。因此,如果您将该行代码移动到您的路由调用中,那么每次调用该路由时它都会被刷新。
例如
@api.route('/display', methods=['POST', 'GET'])
def display_test():
order_test = Order.query.all()
return jsonify(json_list=[i.serialize for i in order_test]), '200'
从你到目前为止所说的看来,无论新数据发送到 web 挂钩多少次,你似乎只能向数据库添加一条新记录,如果你重新启动 API 然后你回到原点,新记录不再存在。
对我来说,在 webhook 中提交事务似乎是一个问题,因为在调用 db.session.add() 之后,数据不会保存到db,因此事务保持打开状态,因此当添加新数据时,它可能会覆盖上一次调用的数据,然后当您结束 API 时,事务要么 committed 或 rollbacked(不记得 flask-alchemy 的默认操作)。您可能需要检查数据本身并查看调用 webhook 后第 51 行中正在 return编辑的数据,并查看在将新数据发送到 webhook 后它是否发生变化。
如果您还比较上面的 webhook 代码和下面的提交和 return 行是不同的。在你的中,它们位于不同的制表符行上,并且在 webhook 函数之外,并且在调用 webhook 时不会得到 运行,因此会有一个开放的事务。
@webhook.route('/order/insert', methods=['POST'])
def insert_orders():
soda_json = request.json
db.session.add(Order(
order_id=soda_json['id'],
created_at=datetime.datetime.now(),
currency=soda_json['currency'],
total_line_items_price=soda_json['total_line_items_price'],
refunds=sum(float(i) for i in soda_json['refunds'] if soda_json['refunds']),
shipping_lines_price=sum([float(line['price']) for line in soda_json['shipping_lines']]),
note=soda_json['note']
))
db.session.commit()
return '200'
查询中的方法顺序似乎有问题。
from my_app.models import Order
order_test = Order.query.all()
这是教程中的结构(https://pythonhosted.org/Flask-SQLAlchemy/queries.html#querying-records),但看起来可能只是查看原始导入模型中的数据。请随时纠正我。
在 flask shell 中的类似操作中,我已经成功地在使用此查询结构提交后立即获取实时数据:
db.session.query([model]).all()
因此 API 方法的一个工作示例可能是:
@api.route('/display', methods=['POST', 'GET'])
def display_test():
order_test = db.session.query(Order).all()
return jsonify(json_list=[i.serialize for i in order_test]), '200'