Key Path Element Must be complete

余生长醉 提交于 2020-01-16 18:13:24

问题


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.


回答1:


I believe entity['collection_key'] = key is the culprit (key is datastore_client.key('VideosToCollections') which has no ID hence the path is incomplete). Perhaps you meant to set it to the key of v. So entity['collection_key'] = v.key



来源:https://stackoverflow.com/questions/55620409/key-path-element-must-be-complete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!