How to ensure that my entity will never be an orphan?

一曲冷凌霜 提交于 2019-12-13 05:51:38

问题


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.



来源:https://stackoverflow.com/questions/37283252/how-to-ensure-that-my-entity-will-never-be-an-orphan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!