简体   繁体   English

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

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

Hi I am developing an application in python3 using the flask frame work that is going to run on appengine standard that uses cloud datastore for persistence嗨,我正在使用 flask 框架在 python3 中开发一个应用程序,该框架将在使用云数据存储进行持久性的 appengine 标准上运行

I want to perform transactions我要进行交易

so I tried the following i所以我尝试了以下我

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

In the local development environment when I hit the handler /test_req I am getting the following error在本地开发环境中,当我点击处理程序 /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.

when I remove the @ndb.transactional() decorator entities gets updated and there is no error当我删除 @ndb.transactional() 装饰器实体得到更新并且没有错误

In the original Cloud Datastore the only queries allowed in transactions are ancestor queries.在原始 Cloud Datastore 中,事务中唯一允许的查询是祖先查询。 The emulator is still enforcing this restriction.模拟器仍在执行此限制。 This change is noted in https://cloud.google.com/datastore/docs/upgrade-to-firestore .https://cloud.google.com/datastore/docs/upgrade-to-firestore中记录了此更改。

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

相关问题 包括 Google Cloud Datastore Python3 AttributeError - Google Cloud Datastore Python3 AttributeError on include PyCharm 中 ndb Google Cloud Datastore 的类型提示丢失 - Typehints for ndb Google Cloud Datastore in PyCharm are missing 使用 Python3x 的 Google Cloud Datastore 中的 UTF-8 字符串? - UTF-8 strings in the Google Cloud Datastore using Python3x? 如何使用Python客户端库将数据批量上传到Google Cloud Spanner? - How do I batch upsert data into Google Cloud Spanner using the Python client library? 如何在 App Engine 中使用 Python3 连接到 Google Cloud Platform 中的 SQL 实例 - How to connect to SQL instance in Google Cloud Platform using Python3 in App Engine 如何在python3标准应用引擎项目中访问谷歌云库? - How to access Google cloud library in python3 standard app engine project? 单元测试为Google AppEngine灵活环境编写的python 3.x代码并使用Cloud Datastore Api - Unit testing a python 3.x code written for Google AppEngine flexible environment and using Cloud Datastore Api 是否有独立 Python3 的谷歌云翻译示例? - Is there an example of Google cloud translate for standalone Python3? 如何在谷歌云数据中激活python3 - how to activate python3 in Google-cloud-data 如何在Google Cloud Platform中为Node应用程序安装Python3 - How to install Python3 in Google Cloud Platform for a Node app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM