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/