繁体   English   中英

如何在 python3 中使用 google-cloud-ndb 库在谷歌云数据存储上进行交易

[英]How to do transactions using on google cloud datastore using google-cloud-ndb library in python3

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

我要进行交易

所以我尝试了以下我

@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() 装饰器实体得到更新并且没有错误

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

暂无
暂无

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

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