如何确保我的实体永远不会是孤儿? [英] How to ensure that my entity will never be an orphan?

查看:157
本文介绍了如何确保我的实体永远不会是孤儿?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,你创建了一个应该存在于另一个实体中的实体类型。但是,如果它变成孤儿,它就没有理由再存在于数据存储中。


在删除祖先后子数据存储对象会发生什么?



根据上面的链接,


  1. 子实体不会在祖先删除

  2. child_entity.key.parent()。get()将返回None。


如果我删除了祖先,那么这个孩子将没有父母,因此它是一个孤儿。



这是一个问题,因为没有任何理由让它停留在数据存储区中。



有什么方法可以确保这种情况在数据库中不会发生?



我能想到的可能解决方案有:


  1. 常规运行宏以删除孤儿

  2. 尝试清理可能会导致我的孩子变成孤儿的代码/除掉错误

然而,我希望更多的编程正确的解决方案,如可以设置的属性或属性,以确保父(关键)永远不会指向不存在的实体。 (也就是当祖先被删除时自动删除实体)



是否有?
如果是,那是什么?
如果不是,为什么不呢?

解决方案

子实体可以永不 成为根实体,因为它继续拥有相同的父键,即使父母被删除(或从不存在)。



实体的父键不能在实体的因为父键被嵌入到实体的键中。



对于删除实体时自动删除实体的后代 - 不存在这种方式。但可以通过编程实现,请参见如何删除包括所有子项的实体


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?

解决方案

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.

这篇关于如何确保我的实体永远不会是孤儿?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆