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?
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With