数据存储区是否可以生成覆盖我的实体的ID?

时间:2014-07-07 14:30:46

标签: python google-app-engine google-cloud-datastore

我有一个root实体,我想重构一个父实体。

这些实体都有自动分配的ID。

我克隆了所有现有实体,从父实体和旧实体的id创建了克隆的密钥。然后我删除了旧实体。

创建的任何新实体都会从数据存储中获取自动分配的ID。

The docs say:

  

数据存储区的自动ID分配器永远不会将属于现有实体的密钥分配给新实体。

...但我不确定这是否仅适用于预先分配的ID的上下文。

我创建的新实体是否会覆盖现有实体? 或者,尽管存在父级差异,数据存储区仍然会将这些ID识别为已使用过吗?

我是否必须预先分配所有现有实体ID,以防止我的实体被新实体覆盖?

编辑:

我的旧实体有这样的键:

datastore_types.Key.from_path(u'MyKind', 123456789, _app=u's~my-app')

我的新克隆实体有这样的键(重用旧ID): datastore_types.Key.from_path(u'ParentKind', 28882914L, u'MyKind', 123456789, _app=u's~my-app')

尽管父母不同,数据存储仍然会认为这个id已被用于此类吗?

3 个答案:

答案 0 :(得分:1)

数据存储区不会认为已使用该ID。当数据存储区分配id时,它从一个序列中分配,该序列由Kind 确定(参见模型参数here)。由于您的新模型具有不同的父模型,因此它们将从自己的ID序列进行分配,这可能会与您手动设置的ID冲突。

仅保证自动创建的ID不会与自动创建的其他ID冲突(使用未设置ID的put或使用allocate_ids方法)。

答案 1 :(得分:0)

如果我做对了,旧实体将拥有以前的ID +父ID,对吗?新实体也将拥有父ID。在这种情况下,Datastore不会覆盖任何实体,因为它永远不会分配重复的密钥。

答案 2 :(得分:0)

allocate_id_range的{​​{3}}的描述来看,似乎可以肯定地说,在使用自动ID分配器时,数据存储区永远不会用新实体覆盖现有实体。

various possible return values

KEY_RANGE_EMPTY = "Empty"
"""Indicates the given key range is empty and the datastore's
automatic ID allocator will not assign keys in this range to new
entities.
"""

KEY_RANGE_CONTENTION = "Contention"
"""Indicates the given key range is empty but the datastore's
automatic ID allocator may assign new entities keys in this range.
However it is safe to manually assign keys in this range
if either of the following is true:

 - No other request will insert entities with the same kind and parent
   as the given key range until all entities with manually assigned
   keys from this range have been written.
 - Overwriting entities written by other requests with the same kind
   and parent as the given key range is acceptable.

The datastore's automatic ID allocator will not assign a key to a new
entity that will overwrite an existing entity, so once the range is
populated there will no longer be any contention.
"""

KEY_RANGE_COLLISION = "Collision"
"""Indicates that entities with keys inside the given key range
already exist and writing to this range will overwrite those entities.
Additionally the implications of KEY_RANGE_COLLISION apply. If
overwriting entities that exist in this range is acceptable it is safe
to use the given range.

The datastore's automatic ID allocator will never assign a key to
a new entity that will overwrite an existing entity so entities
written by the user to this range will never be overwritten by
an entity with an automatically assigned key.
"""

此其他StackOverflow答案同意:From the SDK: