Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get auto generated id from Google Cloud DataStore

The documentation here says that I can omit the numeric id of an entity and DataStore will assign one automatically.

It however, does not say how to retrieve the auto generated id. How does one get it?

Is it available in the response or do I have to make a query against some other fields to retrieve the entity again just so I can see its ID?

like image 217
pdeva Avatar asked Oct 20 '22 11:10

pdeva


1 Answers

It will be in the corresponding MutationResult in the response. Here's a Python snippet that expands on the one in the docs:

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]
like image 90
Ed Davisson Avatar answered Dec 07 '22 12:12

Ed Davisson