[英]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.