-->

How to ensure that my entity will never be an orph

2019-09-05 21:47发布

问题:

Sometimes, you make an entity kind that is supposed to exist in another entity. However, if it turns into an orphan, it will have no reason to exist in the datastore anymore.

What happens to child datastore objects after deleting the ancestor?

According to the link above,

  1. "Child entities do not get deleted when the ancestor is deleted"
  2. "child_entity.key.parent().get() will return None."

If I delete the ancestor, the child will have no parent, making it an orphan.

This is a problem, as there is no reason for it to stay in the datastore anymore.

Is there any way to ensure this never happens in the database?

Possible solutions I can think of are:

  1. Routinely run macros to delete orphans
  2. Try to clean the code/weed out bugs that may cause my child to turn into an orphan

However, the I'm hoping for a more programmatically correct solution like an attribute or property that can be set to ensure me that the parent(key) will never point an entity that doesn't exist. (aka automatically delete the entity when ancestors are deleted)

Is there? If yes, what is it? If no, why not?

回答1:

A child entity can never become a root entity, since it continues to have the same parent key, even if the parent was deleted (or never existed).

An entity's parent key can not be changed during the entity's lifetime since the parent key is embedded in the entity's key.

As for automatically removing an entity's descendents when the entity is deleted - there is no such way. But it can be achieved programatically, see How to delete an entity including all children.