views:

65

answers:

1

I have 2 models:

class Parent (db.Model) :
  data = db.StringProperty()


class Child1 (db.Model) :
  parent = db.ReferenceProperty(Parent)
  childData = db.StringProperty()

class Child2 (db.Model) :
  parent = db.ReferenceProperty(Parent)
  childData2 = db.StringProperty()

class Child3 (db.Model) :
  parent = db.ReferenceProperty(Parent)
  childData3 = db.StringProperty()

....

I want a query which can give me a list of all parents which do not have a child yet. How do i do it?

I do not want to maintain an identifier in the Parent model for each of the children as I want to add new child models very often.

+1  A: 

The correct way to handle this is to keep a count in your Parent entity of the number of children. Then, you can simply query for Parents that have child count equal to 0.

You don't need to have the child id in the Parent entity; you want to keep a count of the number of children. It is not necessarily important whether the child is of one Model type or another. The purpose of keeping this running count is that it enables you to solve your problem, which is querying for childless Parents. There is no native capability of the datastore or of AppEngine query objects that provides this functionality.

So since this is not supported natively, you have the choice of doing it one of two ways:

1) keep a running count of the number of children, incrementing or decrementing whenever you add or remove a child;

2a) query EVERY child of every type and record unique values of parent.

2b) query all Parent entities and match them up with the unique parent ids from 2a

I'll leave it to you to decide which approach is the correct one.

Adam Crossland
updated my question. I do not want to add a child id in the parent .
demos
@demos, updated my answer to match your updated question
Adam Crossland