谷歌应用引擎返回"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()发布于 2012-05-19 09:10:51
这仅仅意味着您使用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)这意味着您首先获取对计数器的引用,但只获取并将该值放入事务性方法中,从而保证原子性。
https://stackoverflow.com/questions/10649767
复制相似问题