Reputation: 7448
I tried to add a collection of entities into datastore using a list of dictionaries, the code is as follows,
def add_rows(self, val_dicts):
with self.client.transaction():
entities = [Entity(self.client.key(self.kind))] * len(val_dicts)
for entity, update_dict in zip(entities, val_dicts):
entity.update(update_dict)
self.client.put_multi(entities)
when I ran the code, I got the following error,
ValueError: Only a partial key can be completed.
But if I changed the code to,
def add_rows(self, val_dicts):
with self.client.transaction():
entities = [Entity(self.client.key(self.kind)) for i in range(len(val_dicts))]
for entity, update_dict in zip(entities, val_dicts):
entity.update(update_dict)
self.client.put_multi(entities)
the error is gone. But there is no difference in creating the entities
, so I am wondering what is the issue in the first code snippet.
I am also using datastore emulator
for testing these code.
Upvotes: 1
Views: 304
Reputation: 39824
In your first code snippet you only create a single entity and entities
is a list containing identical references to that single entity - because of the way the list is initialized. Which is likely the root cause of your problem - you're overwriting that entity with each val_dicts
value and put_multi()
barfs because it's called with the same entity multiple times as arguments.
In the second snippet each entities
element is a diferent entity, no problem.
To illustrate:
>>> class Foo(object):
... pass
...
>>> [Foo()]*2
[<__main__.Foo object at 0x7fc9760eee80>, <__main__.Foo object at 0x7fc9760eee80>]
>>> [Foo() for i in range(2)]
[<__main__.Foo object at 0x7fc9760eeeb8>, <__main__.Foo object at 0x7fc9760eefd0>]
>>>
Note the identical object addresses in the 1st case.
Upvotes: 2