I am having deletes "intermittently" fail -- without error.  When I
check the number of records that will be deleted (by printing out
immediately before deletion) it is correct.  I call db.delete() and
receive no errors, but the entities are not deleted.  A "snippet" of
the problem code:
db.delete(MyChildModel.all(keys_only=True).ancestor
(myParentModelInstance))

Anyone have any thoughts?


This is a more complete discussion of the problem followed by more
complete code:

Below is a slightly condensed version of the code.  Basically the
handler is called, and a group instance is either created or fetched,
by key, from the datastore at the top of the post handler.  The posted
variables are then validated and assigned to the corresponding "group"
instance properties.

One of the variables is a list of "members."  I validate each entry to
verify that the supplied key is valid and of the correct kind.  Then I
attempt to save the group and "child" group-member entries.  In this
case I know all groups are very small typically 4 to 6 members, never
over 10.  Instead of a "lookup" to figure out who is and is not still
in the group I just wipe out any existing "groupmember" entries and
recreate them all, for the current group.

This is where the issue comes in.  Sometimes the GroupMember records
are deleted and sometimes they are not.  It seems to work almost
randomly on both the development and production servers.  However, on
the development server, simply re-saving the file with NO code changes
causes a recompile and it works every time.  On the production
servers, if I wait a few minutes then hit save it works every time.
It seems like each time the file is "freshly" loaded it works.


class GroupEditHandler(webapp.RequestHandler):
  def post(self):
    # get post vars and build group instance here
    groupKey = self.request.get('groupKey')
    if groupKey:
      try:
        group = Group.get(db.Key(groupKey))
      except:
        self.handleError()
        return
    else:
      group = Group(organization=self.session
['organizationKey'],division=self.session['divisionKey'])

    #....  fetch and set other varaibles here ....#

    memberValues = self.request.get('members',allow_multiple=True)
    # loop over each memberKey and validate it.
    for member in memberValues:
      # be sure it at least has something in it
      if member:
        try:
          # Convert variable to a db.Key and look it up through the
          # Person kind to be sure it is the correct type.
          member = Person.get(db.Key(member))
          if member:
            # It is good, add it to the temp member list.
            group._members.append(member)
          else:
            # handle invalid member
        except:
          # handle member lookup related failure

    self.saveGroup(group)


  def saveGroup(self,group):
    def txn():
      key = None
      key = group.put()
      if key:
        # handle name search object and tag search objects here

        if group._members:
          # Just wipe all members out, the membership is small (<10
members / group)
          # This is the problem line.  No errors, the items are being
returned
          # ... they are just not being deleted consistently.
          db.delete(groupMember.all(keys_only=True).ancestor(group))

          # Build a new set of members and write it.
          db.put([groupMember(parent=group,
                                              organization=self.session
['organizationKey'],
                                              division=self.session
['divisionKey']),
                                              member=member,
                                              memberName=member.name)
                            for member in group._members])
        return True
      return False
    return db.run_in_transaction(txn)



# Models, for reference
class Group(db.Expando):
    owner = db.UserProperty(auto_current_user_add=True)
    organization = db.ReferenceProperty
(reference_class=Organization,required=True)
    division = db.ReferenceProperty
(reference_class=Division,required=True)

    name = db.StringProperty(multiline=False)
    active = db.BooleanProperty(default=True,required=True)

    # Place holders
    _search = []
    _tags = []
    _members = []

    # If we just call organization.key() the organization will be
dereferenced,
    #  costing us a datastore call (and time)
    def organizationKey(self):
        """ Needed to avoid dereffing the organization to get the key
"""
        return Group.organization.get_value_for_datastore(self)

    def divisionKey(self):
        """ Needed to avoid dereffing the division to get the key """
        return Group.division.get_value_for_datastore(self)


class GroupMember(db.Model):
    organization = db.ReferenceProperty
(reference_class=Organization,required=True)
    division = db.ReferenceProperty
(reference_class=Division,required=True)
    member = db.ReferenceProperty
(reference_class=Person,required=True)
    memberName = db.StringProperty(multiline=False)

    def memberKey(self):
        """ Needed to avoid dereffing the member to get the key """
        return GroupMember.member.get_value_for_datastore(self)





--~--~---------~--~----~------------~-------~--~----~
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 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to