Good catch! You're right, the code sample does not match the description. The intent was for the new object to be created with a key name based on account_id. I'll fix it. Thanks! -- Dan
On Wed, Oct 29, 2008 at 2:17 AM, an0 <[EMAIL PROTECTED]> wrote: > > > http://code.google.com/appengine/docs/datastore/transactions.html#Uses_For_Transactions > > >> Another common use for transactions is to update an entity with a named > >> key, or create it if it doesn't yet exist: > >> > >> class SalesAccount(db.Model): > >> address = db.PostalAddressProperty() > >> phone_number = db.PhoneNumberProperty() > >> > >> def create_or_update(parent_obj, account_id, address, phone_number): > >> obj = db.get(Key.from_path("SalesAccount", account_id, > parent=parent_obj)) > >> if not obj: > >> obj = SalesAccount(parent=parent_obj, > >> address=address, > >> phone_number=phone_number) > >> else: > >> obj.address = address > >> obj.phone_number = phone_number > >> > >> obj.put() > >> > >> As before, a transaction is necessary to handle the case where another > user > >> is attempting to create or update an entity with the same account_id. > >> Without a transaction, if the entity does not exist and two users > attempt > >> to create it, the second will fail. With a transaction, the second > attempt > According to Paths and Key Uniqueness(http://code.google.com/appengine/ > docs/datastore/keysandentitygroups.html#Paths_and_Key_Uniqueness<http://code.google.com/appengine/docs/datastore/keysandentitygroups.html#Paths_and_Key_Uniqueness> > ), > just same kind and same parent do no mean same key, since the ids or > key names differ. Without the same key, obj2 should be put without > clash, even with same properties of obj1. Thus, the second should not > fail even without a transaction. > >> will retry, notice that the entity now exists, and update the entity > >> instead. > > > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google App Engine" group. To post to this group, send email to google-appengine@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en -~----------~----~----~----~------~----~------~--~---