Ajax
Ajax

Reputation: 1512

gql inside transactions

Google app engine returns "BadRequestError: Only ancestor queries are allowed inside transactions." What does this mean in the context of code:

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

Upvotes: 1

Views: 159

Answers (1)

Aert
Aert

Reputation: 2049

It simply means that the query you run with Counter.all().get() is not an ancestor query. In this case, you should take the query that fetches the counter out of the transactional method, like this:

@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)

This means you first get a reference to the Counter, but only get and put the value in the transactional method, guaranteeing atomicity.

Upvotes: 3

Related Questions