Self-references

An entity can relate to itself using a self-reference relationship. Such relationships can be of two types: symmetric and non-symmetric. A non-symmetric relationship is defined by two attributes which belong to the same entity.

The specifics of the symmetrical relationship is that the entity has just one relationship attribute specified, and this attribute defines both sides of the relationship. Such relationship can be either one-to-one or many-to-many. Here are examples of self-reference relationships:

  1. class Person(db.Entity):
  2. name = Required(str)
  3. spouse = Optional("Person", reverse="spouse") # symmetric one-to-one
  4. friends = Set("Person", reverse="friends") # symmetric many-to-many
  5. manager = Optional("Person", reverse="employees") # one side of non-symmetric
  6. employees = Set("Person", reverse="manager") # another side of non-symmetric