如何从 Google Cloud DataStore 获取自动生成的 ID

pde*_*eva 5 google-cloud-datastore google-cloud-platform

此处的文档说我可以省略实体的数字 id,DataStore 将自动分配一个。

但是,它没有说明如何检索自动生成的 ID。怎么得到呢?

它是否在响应中可用,或者我是否必须对其他一些字段进行查询以再次检索实体,以便我可以看到它的 ID?

Ed *_*son 3

它将在MutationResult响应中的相应内容中。以下是对文档中的代码进行扩展的 Python 代码片段:

req = datastore.CommitRequest()
req.mode = datastore.CommitRequest.NON_TRANSACTIONAL
employee = req.mutation.insert_auto_id.add()

# Request insertion with automatic ID allocation
path_element = employee.key.path_element.add()
path_element.kind = 'Employee'

# ... set properties ...

resp = self.datastore.commit(req)

auto_id_key = resp.mutation_result.insert_auto_id_key[0]
Run Code Online (Sandbox Code Playgroud)