python - GQL 内部交易

标签 python google-app-engine

Google 应用引擎返回“BadRequestError:事务内只允许祖先查询。”这在代码上下文中意味着什么:

class Counter(db.Model):
        totalRegistrations = db.IntegerProperty(default=0)   

@db.transactional
def countUsers():
    counter = Counter.all().get()
    counter.totalRegistrations = counter.totalRegistrations + 1
    counter.put()
    i = counter.totalRegistrations
    return i

print countUsers()

最佳答案

这仅意味着您使用 Counter.all().get() 运行的查询不是祖先查询。在这种情况下,您应该从事务方法中获取获取计数器的查询,如下所示:

@db.transactional
def incrementUsers(counterKey):
    counter = Counter.get(counterKey)
    counter.totalRegistrations = counter.totalRegistrations + 1
    counter.put()
    return counter.totalRegistrations

counterKey = Counter.all(keys_only=True).get()

print incrementUsers(counterKey)

这意味着您首先获得对 Counter 的引用,但仅在事务方法中获取和放置该值,从而保证原子性。

关于python - GQL 内部交易,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10649767/

相关文章:

python - 当列更改 numpy.array 中的值时如何选择第一行

python - 设置 App Engine mapreduce 分片大小

python - 尝试通过AWS Lambda连接到Google Analytics API,这个 "ModuleNotFoundError: No module named ' google.appengine'“是什么意思

Python正则表达式匹配给定字符串中的多个模式

python - 通过 Selenium 启动 Chrome

python - 在 Windows 上使用 Scipy 的 AMD64 版本调用 scikit-learn 时出错

python - 如何在 python 中从一个 .py 文件运行多个 .py 文件

java - GAE : How to map object oriented designs into Appengine datastore efficiently

python - Web/Screen Scraping with Google App Engine - 代码在 python 解释器中工作,但在 GAE 中不工作

java - 如何处理 Google App Engine 中的 session ?