将父项分配给现有实体 [英] Assign a parent to an existing entity
问题描述
我有一个已经填充了实体的数据存储。但是,他们还没有被安排到实体组织中(又称亲子关系)。我的实体Kinds是:
$ $ p $ code team = Team.get_by_key_name('Plants')
query = Plants.all()
Plants
团队尚未完成:
query = Plants.all()。祖先(团队)
植物= query.fetch(50)
#这将导致一个空的列表
我想现在将它们分配给团队:
query = Plants.all()
植物= query.fetch(50)
植物中的p:
#这是不正确的
p.parent =团队
db.put(plants)
当我尝试再次基于祖先
:
query = Plants.all()。ancestor(团队)
植物= query.fetch(50)
#这仍然会导致一个空的列表
问题:如何将父项分配给已存在的实体?
$ b
这是因为父母成为实体密钥的一部分(因此,为什么您需要将任何现有的父母传递给 db.Key
或 get_by_key_name
等等),所以它在你描述的方式(或者至少不是我所知道的)方面是不可改变的。
编辑:
Xion带来了一个我忘记提及的好点。当用新的对象替换之前的对象时,还必须处理任何 ReferenceProperty
和 ListProperty(db.Key)$ c $可能指向旧对象。我想这一天的教训是在放下任何数据之前更仔细地设计和考虑实体组。
I have a datastore which is already populated with entities. However, they haven't been arranged into entity groups (a.k.a parent-child relationships). My entity Kinds are:
team = Team.get_by_key_name('Plants')
query = Plants.all()
The plants
haven't been assigned to the Plants
team yet:
query = Plants.all().ancestor(team)
plants = query.fetch(50)
# This will result in an empty list
I would like to assign them to the team now:
query = Plants.all()
plants = query.fetch(50)
for p in plants:
# This isn't right
p.parent = team
db.put(plants)
When I try to query again based on ancestor
:
query = Plants.all().ancestor(team)
plants = query.fetch(50)
# This still results in an empty list
Question: How do I assign a parent to an already existing entity?
In my experience the easiest way to do this is to create a new entity copied with all the exact same info, and set the parent appropriately on this new entity.
This is because the parent becomes part of an entity's key (hence why you need to pass any existing parent to db.Key
, or get_by_key_name
, etc for them to work properly), so it's not changeable in the way you describe (or at least, not as far as I know).
Edit:
Xion brings up a good point that I forgot to mention. When replacing the previous object with the new one, you also have to take care of any ReferenceProperty
and ListProperty(db.Key)
that may have pointed to the old object. I guess the lesson of the day here is to design and think about entity groups more carefully before putting down any data.
这篇关于将父项分配给现有实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!