Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数据存储能否生成覆盖我的实体的ID?_Python_Google App Engine_Google Cloud Datastore - Fatal编程技术网

Python 数据存储能否生成覆盖我的实体的ID?

Python 数据存储能否生成覆盖我的实体的ID?,python,google-app-engine,google-cloud-datastore,Python,Google App Engine,Google Cloud Datastore,我有一个根实体,我想重构它来拥有一个父实体 这些实体都有自动分配的ID 我克隆了所有现有实体,从父实体和旧实体的id创建克隆的密钥。然后我删除了旧的实体 创建的任何新实体都会从数据存储中自动分配ID 数据存储的自动ID分配器从不将属于现有实体的密钥分配给新实体 …但我不确定这是否只适用于预分配ID的上下文 创建的新实体能否覆盖现有实体? 或者,尽管存在父级差异,数据存储是否仍会将这些ID识别为已使用的ID 我是否必须预先分配所有现有实体ID以防止新实体覆盖我的实体 编辑: 我的旧实体有如下键:

我有一个根实体,我想重构它来拥有一个父实体

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

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

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

数据存储的自动ID分配器从不将属于现有实体的密钥分配给新实体

…但我不确定这是否只适用于预分配ID的上下文

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

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

编辑:

我的旧实体有如下键:

datastore\u types.Key.from\u path(u'MyKind',123456789,\u app=u's~my app')

我的新克隆实体具有如下密钥(重用旧id):
datastore\u types.Key.from\u path(u'ParentKind',28882914L,u'MyKind',123456789,\u app=u's~my app')


那么,尽管父对象不同,数据存储是否仍会认为此id已用于此类对象?

如果我没有弄错,旧实体将具有以前的id+父对象id,对吗?新实体也将具有父Id。在这种情况下,数据存储不会覆盖任何实体,因为它从不分配重复的密钥。

从分配Id范围的描述判断,可以肯定地说,在使用自动ID分配器时,数据存储永远不会用新实体覆盖现有实体


另一个StackOverflow答案也表示同意:

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


自动创建的id仅保证不会与自动创建的其他id发生冲突(使用未设置id的
put
或使用该方法)

如果自动创建的id与手动设置的id冲突,会发生什么情况?实体似乎永远不会被覆盖,那么会发生什么呢?实体的创建将失败?在这种情况下,
put
调用将失败,id将被标记为已使用。它将永久失败还是自动重试?当我尝试使用
allocate\u id\u range
分配任何这些手动分配的id时,我得到的错误
超过了生产上分配的最大id
。在SDK中,当我做同样的事情时,我得到了
KEY\u RANGE\u COLLISION
。我想,如果有2**53个ID的选择,并且这些ID是按种类和父项命名的,那么任何ID都不太可能发生冲突,但我仍然希望消除这种可能性。关于我应该做什么有什么建议吗?put本身将立即失败(取决于您使用的库,它可能会重试)。但是,如果您再次调用
put
,它将使用新id并成功。我建议(如果可能的话)重新放置您当前的实体并让它们获得新的ID,而不是使用现有的ID。如果这不是一个选项,我会添加您自己的重试逻辑。
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.
"""