2009-06-02 90 views
5

遞歸關係在一個典型的關係型數據庫,我有如下表:與谷歌App Engine和BigTable的

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

我想這個表格轉換成谷歌App Engine的表。我的問題是與MotherId和FatherId字段。我嘗試了下面的代碼,但沒有機會。 Python說它不知道對象類型Person。

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

有人知道我們如何在Google App Engine表中建模遞歸關係嗎?我該如何解決App Engine的限制?

UPDATE 我想擴大這個問題一點點...如果我想添加一個孩子的集合?

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

我試過了,它不起作用。任何想法我做錯了什麼?

謝謝!

回答

10

我認爲你要SelfReferenceProperty這裏

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

或者,你可以在不同的類中的母親和父親的關係。

+1

您還需要設置集合名稱屬性,以避免「類人已經擁有財產person_set」錯誤: 媽媽= db.SelfReferenceProperty(集合名稱=「mother_set」) 父親= db.SelfReferenceProperty(集合名稱=」 father_set') – robertc 2009-06-02 15:03:04