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

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

问题描述

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

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.

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

根据上面的链接,

  1. 删除祖先时不会删除子实体"
  2. child_entity.key.parent().get() 将返回 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. 定期运行宏来删除孤儿
  2. 尝试清理代码/清除可能导致我的孩子变成孤儿的错误

但是,我希望有一个更编程正确的解决方案,例如可以设置的属性或属性,以确保父(键)永远不会指向不存在的实体.(也就是删除祖先时自动删除实体)

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天全站免登陆