Search code examples
google-cloud-platformnosqlgoogle-cloud-datastoresql-update

Problem with updating only some fields of entities in Google Cloud Datastore


I created an entity in Google Cloud Datastore using Python this way:

client = datastore.Client()

key = client.key('EntityType', id)
entity = datastore.Entity(key=key)
entity.update({'id': id, 'property_0': value_0, 'property_1': value_1,})

After this, when I check my entities list, I have a new entity with the 3 properties id, property_0 and property_1

In another function, I only updated property_2 which I do so this way

key = client.key('EntityType', id)
entity = datastore.Entity(key=key)
entity.update({'property_1': new_value_1,})

When I check the entities list after this, I only see property_2 of my entity with the new value.

How can I update only property_1 of the entity while still keeping the other ones?


Solution

  • You need to fetch it first

    key = client.key('EntityType', id)
    entity = client.get(key)
    entity.update({'property_2': new_value_2,})