gql内部事务

2024-09-25 00:28:20 发布

您现在位置:Python中文网/ 问答频道 /正文

Google app engine返回“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()

Tags: 代码appdefaultdbmodelgooglecounter事务
1条回答
网友
1楼 · 发布于 2024-09-25 00:28:20

这仅仅意味着用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)

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

相关问题 更多 >