1

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()
4

1 回答 1

3

它只是意味着您运行的查询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 的引用,但仅在事务方法中获取并放入值,从而保证原子性。

于 2012-05-19T01:10:50.563 回答