Flask-Sqlalchemy:数据库查询不返回新数据 [英] Flask-Sqlalchemy: DB queries don't return new data

查看:98
本文介绍了Flask-Sqlalchemy:数据库查询不返回新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,该应用程序从一项服务接收Webhook,将数据存储在数据库中,然后通过API使数据可用.

I'm building an app that receives webhooks from one service, stores the data in a database, then makes the data available via API.

我能够成功将数据添加到我的应用程序中,但是当我查询数据库时,我仅收到上次启动应用程序时数据库中的第一次提交.

I'm able to successfully add data to my app, but when I query the database I only receive the first commit from the that was in the database the last time I started the app.

例如,如果我在启动应用程序时在订单"表中有26个订单,则触发Webhook, Order.query.all()将返回27个订单,直到我重新启动应用程序为止,不管表中实际上有多少订单(我可以使用MySQL进行验证).

For example, if I had 26 orders in the Orders table when I booted the app, then trigger the webhook, Order.query.all() will return 27 orders until I reboot the app, regardless of how many orders are actually in the table (I can validate using MySQL).

这是用于向表中插入数据的类的示例:

Here's an example of the class used to insert data into the 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方法:

And here's a basic API method I'm using for testing:

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'

要始终获取最新数据,我缺少什么?

What am I missing to always get the most recent data?

推荐答案

看起来查询中方法的顺序可能是个问题.

It looks like the order of methods in the query could be an issue.

from my_app.models import Order

order_test = Order.query.all()

这就是本教程中的结构( https://pythonhosted.org/Flask-SQLAlchemy/queries.html#querying-records ),但似乎只能查看原始导入模型中的数据.随时对此进行纠正.

That is the structure in the tutorial ( https://pythonhosted.org/Flask-SQLAlchemy/queries.html#querying-records ), but it seems like that might only be looking at data in the original imported model. Feel free to correct me on that.

在flask外壳中的类似操作中,使用此查询结构提交后,我已经成功获取了实时数据:

In similar operations in the flask shell, I've had success getting live data right after commits with this query structure:

db.session.query([model]).all()

因此,API方法的有效示例可能是:

So a working example for API method might be:

@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'

这篇关于Flask-Sqlalchemy:数据库查询不返回新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆