5

In a classic relational database, I have the following table:

CREATE TABLE Person(
    Id int IDENTITY(1,1) NOT NULL PRIMARY KEY,
    MotherId int NOT NULL REFERENCES Person(Id),
    FatherId int NOT NULL REFERENCES Person(Id),
    FirstName nvarchar(255))

I am trying to convert this table into a Google App Engine table. My issue is with the fields MotherId and FatherId. I tried the code below, but no chance. Python says that it doesn't know the object type Person.

class Person(db.Model):
    mother = db.ReferenceProperty(Person)
    father = db.ReferenceProperty(Person)
    firstName = db.StringProperty()

Does someone know how we can model a recursive relationship in a Google App Engine table? How could I work around the limitation of App Engine?

UPDATE I want to expand the problem a little bit... What if I wanted to add a collection of children?

children = db.SelfReferenceProperty(collection_name='children_set')
dad.children.append(childrenOne)

I tried this and it doesn't work. Any idea what I am doing wrong?

Thanks!

Martin
  • 39,309
  • 62
  • 192
  • 278

1 Answers1

10

I think that you want SelfReferenceProperty here

class Person(db.Model):
    mother = db.SelfReferenceProperty(collection_name='mother_set')
    father = db.SelfReferenceProperty(collection_name='father_set')
    firstName = db.StringProperty()

Alternatively, you can put the Mother and Father relations in separate classes.

Nicolas Dumazet
  • 7,147
  • 27
  • 36
  • 1
    You'll also need to set the collection_name property to avoid the 'Class Person already has property person_set' error: mother = db.SelfReferenceProperty(collection_name='mother_set') father = db.SelfReferenceProperty(collection_name='father_set') – robertc Jun 02 '09 at 15:03
  • This is really cool! I didn't know about that. How could I model a collection of children? children = db.SelfReferenceProperty(collection_name='children_set') dad.children.append(childrenOne) I tried this and it doesn't work. :( – Martin Jun 02 '09 at 16:41