I'm working through some documentation for the google cloud datastore API
from google.cloud import datastore
datastore_client = datastore.Client(project="PROJECTNAME")
query = datastore_client.query(kind='Video')
r = query.fetch()
for v in r:
key = datastore_client.key('VideosToCollections')
entity = datastore.Entity(key=key)
entity['collection_key'] = key
datastore_client.put(entity)
quit()
However I receive this error when updating an entry
Traceback (most recent call last):
File "fillvideocollections.py", line 20, in <module> datastore_client.put(entity)
File "/usr/local/lib/python2.7/dist-packages/google/cloud/datastore/client.py", line 421, in put
self.put_multi(entities=[entity])
File "/usr/local/lib/python2.7/dist-packages/google/cloud/datastore/client.py", line 448, in put_multi
current.commit()
File "/usr/local/lib/python2.7/dist-packages/google/cloud/datastore/batch.py", line 274, in commit
self._commit()
File "/usr/local/lib/python2.7/dist-packages/google/cloud/datastore/batch.py", line 250, in _commit
self.project, mode, self._mutations, transaction=self._id
File "/usr/local/lib/python2.7/dist-packages/google/cloud/datastore_v1/gapic/datastore_client.py", line 501, in commit
request, retry=retry, timeout=timeout, metadata=metadata
File "/usr/local/lib/python2.7/dist-packages/google/api_core/gapic_v1/method.py", line 143, in __call__
return wrapped_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/google/api_core/retry.py", line 270, in retry_wrapped_func
on_error=on_error,
File "/usr/local/lib/python2.7/dist-packages/google/api_core/retry.py", line 179, in retry_target
return target()
File "/usr/local/lib/python2.7/dist-packages/google/api_core/timeout.py", line 214, in func_with_timeout
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/google/api_core/grpc_helpers.py", line 59, in error_remapped_callable
six.raise_from(exceptions.from_grpc_error(exc), exc)
File "/home/jeffbrubaker/.local/lib/python2.7/site-packages/six.py", line 737, in raise_from
raise value
google.api_core.exceptions.InvalidArgument: 400 Key path element must not be incomplete: [VideosToCollections: ]
I want the code above to create one VideosToCollections object as a stepping stone, but will expand to creating one per Video. Any thoughts on what is causing the above error would be appreciated.
I believe
entity['collection_key'] = key
is the culprit (key
isdatastore_client.key('VideosToCollections')
which has no ID hence the path is incomplete). Perhaps you meant to set it to the key ofv
. Soentity['collection_key'] = v.key