Skip to content

datastore: Nested entity parsing fails #616

@pdknsk

Description

@pdknsk

https://cloud.google.com/appengine/docs/python/ndb/properties#structured

In order to write entities with structured properties, I tried to read them first, to figure out the format.

First StructuredProperty.

class Item_Meta(ndb.Model):
    xy = ndb.IntegerProperty(repeated = True)

class Item(ndb.Model):
    meta = ndb.StructuredProperty(Item_Meta)

item = Item()
item.meta = Item_Meta(xy = [100, 200])
item.put() # 5710239819104256
>>> datastore.api.get([datastore.Key('Item', 5710239819104256)])
[<Entity[{'kind': u'Item', 'id': 5710239819104256L}] {u'meta.xy': [100L, 200L]}>]

Next LocalStructuredProperty.

Although a StructuredProperty can be repeated and a StructuredProperty can contain another StructuredProperty, beware: if one structured property contains another, only one of them can be repeated. A work-around is to use LocalStructuredProperty, which does not have this constraint.

class Item_Meta(ndb.Model):
    xy = ndb.IntegerProperty(repeated = True)

class Item(ndb.Model):
    meta = ndb.LocalStructuredProperty(Item_Meta, repeated = True)

item = Item()
item.meta = [Item_Meta(xy = [100, 200])]
item.put() # 6217263929622528
>>> datastore.api.get([datastore.Key('Item', 6217263929622528)])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/.local/lib/python2.7/site-packages/gcloud/datastore/api.py", line 229, in get
    entities.append(helpers.entity_from_protobuf(entity_pb))
  File "/home/user/.local/lib/python2.7/site-packages/gcloud/datastore/helpers.py", line 53, in entity_from_protobuf
    value = _get_value_from_property_pb(property_pb)
  File "/home/user/.local/lib/python2.7/site-packages/gcloud/datastore/helpers.py", line 237, in _get_value_from_property_pb
    return _get_value_from_value_pb(property_pb.value)
  File "/home/user/.local/lib/python2.7/site-packages/gcloud/datastore/helpers.py", line 217, in _get_value_from_value_pb
    result = [_get_value_from_value_pb(x) for x in value_pb.list_value]
  File "/home/user/.local/lib/python2.7/site-packages/gcloud/datastore/helpers.py", line 214, in _get_value_from_value_pb
    result = entity_from_protobuf(value_pb.entity_value)
  File "/home/user/.local/lib/python2.7/site-packages/gcloud/datastore/helpers.py", line 48, in entity_from_protobuf
    key = key_from_protobuf(pb.key)
  File "/home/user/.local/lib/python2.7/site-packages/gcloud/datastore/helpers.py", line 105, in key_from_protobuf
    return Key(*path_args, namespace=namespace, dataset_id=dataset_id)
  File "/home/user/.local/lib/python2.7/site-packages/gcloud/datastore/key.py", line 76, in __init__
    self._path = self._combine_args()
  File "/home/user/.local/lib/python2.7/site-packages/gcloud/datastore/key.py", line 133, in _combine_args
    child_path = self._parse_path(self._flat_path)
  File "/home/user/.local/lib/python2.7/site-packages/gcloud/datastore/key.py", line 94, in _parse_path
    raise ValueError('Key path must not be empty.')
ValueError: Key path must not be empty.

Metadata

Metadata

Assignees

Labels

api: datastoreIssues related to the Datastore API.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions