user3776727
user3776727

Reputation: 13

Google NDB update only specific property?

Is there a way to update only specific property on NDB entity?

Consider this example.

Entity A has following property: property B property C

Let's assume both of these properties have values of 1 at the moment.

Two different request are trying to update same entity and they are happening at the same time. So when Request#1 and #2 are retrieving this entity, value of B and C were 1.

Now Request #1 tries to update property B so it sets the value B to 2 and put into Datastore. Now B = 2 and C = 1 in the datastore. But, Request #2 has B=1 and C=1 in the memory and when it change C to 2 and put into DB, it put's B=1 and C=2 which overwrites B value written by Request #1.

How do you get around this? Is there way to only write specific property into datastore?

Upvotes: 0

Views: 413

Answers (1)

Albert
Albert

Reputation: 625

I believe you may want to look into transactions.

As per the documentation:

If the transaction "collides" with another, it fails; NDB automatically retries such failed transactions a few times. Thus, the function may be called multiple times if the transaction is retried.

Link: https://developers.google.com/appengine/docs/python/ndb/transactions

Upvotes: 1

Related Questions