from google.appengine.ext import db
class Parent(db.Model):
app_id = db.StringProperty()
class Child(db.Model):
app_id = db.ReferenceProperty(Parent,collection_name='children')
type = db.StringProperty()
count = db.IntegerProperty()
@db.transactional
def increment_counter(app,childName):
childA = Child.all().ancestor(app).filter('type =',childName).get()
if childA is not None:
obj = db.get(childA.key())
obj.count += 1
obj.put()
else:
childA = Child(app_id=app.key(),type=childName,count=0)
childA.put()
increment_counter(Parent.all().filter('app_id =',app_id).get().key(),childKey)
This fails with the title error on the put() if childA is empty.
In my head I want to attach a new child entity to the single Parent entity I’m working with in the transaction if it’s not already present, otherwise do an increment. Why is this not regarded as an ancestor query and what should I be doing instead to get the effect I’m after?
OK, seems after a time away from App Engine I confused the separate concepts of entity parent and ReferenceProperty.
The correct code: