Petwoip
Petwoip

Reputation: 1345

Is it possible to create references in Google App Engine?

Let's say I have a datastore that contains mother, father, and child objects. Inside the Mother and Father objects I have a field called child which stores a reference to their child. Is it possible to reference this child from both mother and father without creating duplicate child instances for each (in the style of OOP). Is this how databases work at all?

Upvotes: 3

Views: 104

Answers (1)

David Underhill
David Underhill

Reputation: 16243

Yes, you can use the db.ReferenceProperty to do exactly that.

A reference property simply stores the unique key of the entity it references. So the mother and father entities could each contain a copy of the key corresponding to their child like this:

class Child(db.Model):
    ... # child properties
class Father(db.Model):
    child = db.ReferenceProperty(Child)
    ...
class Mother(db.Model):
    child = db.ReferenceProperty(Child)
    ...

Upvotes: 5

Related Questions