I am trying to do the equivalent of an "Update or Insert (if not exists)".
Any idea how to do this? The only solution I can come up with was to do a GQL query, then either update returned records, or create a new one if none was returned from the query.
This method would not be atomic - but I am wondering if there was a "simple" way to do this.
There are no atomic operations as such. You need to use transactions.
How about reading the docs, both db and ndb have a method for 'get or insert' which is transactional and then you do the update/apply of values to properties inside a transaction and then write (put()) the entity.
https://developers.google.com/appengine/docs/python/ndb/modelclass#Model_get_or_insert
Also remember queries results are subject to "eventual" consistency unless they are ancestor queries.
Read more about transactions https://developers.google.com/appengine/docs/python/ndb/transactions
If you have the key, just get, update, and put within a transaction. There's no "update" operation in GAE's datastore, it's just read the entity, and write the entity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With