【问题标题】:How to do transactions using on google cloud datastore using google-cloud-ndb library in python3如何在 python3 中使用 google-cloud-ndb 库在谷歌云数据存储上进行交易
【发布时间】:2020-04-04 20:24:59
【问题描述】:

您好,我正在使用将在 appengine 标准上运行的烧瓶框架在 python3 中开发一个应用程序,该标准使用云数据存储进行持久性

我要进行交易

所以我尝试了以下我

@ndb.transactional()
def update_user(req_data):
    print("running for req")
    print(req_data)
    query = TestUser.query(ndb.AND(TestUser.age=="1"))
    with client.context():
        result = query.get()
        if result.name == "the one":
            print("not writing")
            return
        else:
            print(result.name+ " is not equal to 'the one'")
            print(result.name)
        result.name = req_data["name"]
        result.put()

    print("transaction ended")

@app.route('/test_req',methods=['POST'])
def test_req_handler():
    req_data = request.get_json()
    update_user(req_data)

    print(req_data)
    return "ok"

在本地开发环境中,当我点击处理程序 /test_req 时出现以下错误

\lib\site-packages\google\cloud\ndb\context.py", line 72, in get_context
    raise exceptions.ContextError()
google.cloud.ndb.exceptions.ContextError: No current context. NDB calls must be made in context established by google.cloud.ndb.Client.context.

当我删除 @ndb.transactional() 装饰器实体得到更新并且没有错误

【问题讨论】:

  • 我可以找到非常相似的issue,它被标记为固定。如果可能的话,我建议在那里打开一个带有详细描述和可重现示例的新问题。在那里它将得到适当的处理。

标签: python-3.x google-app-engine google-cloud-datastore


【解决方案1】:

在原始 Cloud Datastore 中,事务中唯一允许的查询是祖先查询。模拟器仍在执行此限制。此更改已在 https://cloud.google.com/datastore/docs/upgrade-to-firestore 中注明。

【讨论】:

    猜你喜欢
    • 2019-01-15
    • 2017-01-22
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    相关资源
    最近更新 更多