首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >事务内部gql

事务内部gql
EN

Stack Overflow用户
提问于 2012-05-18 17:09:04
回答 1查看 144关注 0票数 1

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

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-19 09:10:51

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

代码语言:javascript
复制
@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)

这意味着您首先获取对计数器的引用,但只获取并将该值放入事务性方法中,从而保证原子性。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10649767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档