Inserting two operations into a transaction and receiving "Unable to work with different groups of entities in a transaction" Error

My ultimate goal is simple. I need to have an entity that has two unique indexed fields that can work as keys. If it was an SQL database, equivelant will have two fields that are defined both unique and independent of each other. I know that this function is not directly possible for a single data store. Db.Model, so I had to create a parent-child model script that mimics this behavior.

To solve this problem, I created two models (ParentEntity and ChildEntity.) The ParentEntity model is a dummy db.Model that stores two values ​​of two keys, but only one of the keys is assigned the <strong> key_name of model No. 1.

After creating the parent object, I create a second child object by assigning the second key as key_name and assigning the parent object that I just created to the parent objects in the constructor of the new ChildEntity object.

My guess is that this will keep these entities within the same entity group, because that is what the Google documentation implies.

I added an insert method called InsertData strong> to ParentEntity (which could just as easily fit into ChildEntity), which can be called to control this insertion logic and is trying to insert these records using a transaction.

When I call InsertData strong>, I get the following error:

It is impossible to work on another group object in a transaction: (kind = 'ChildEntity', name = 'key_name> 2') and (kind = 'ParentEntity', name = 'key_name 1').

If my second object (ChildEntity) is assigned the first object (ParentEntity) to the parent parameter , should these two objects not be in the same entity group?

, . , ChildEntity, txn(), .

class ParentEntity(db.Model):
    str1_key =  db.StringProperty()
    str2 =      db.StringProperty()

    @staticmethod
    def InsertData(string1, string2, string3):
        try:
            def txn():
                #create first entity
                prt = ParentEntity(
                    key_name=string1, 
                    str1_key=string1, 
                    str2=string2)
                prt.put()

                #create User Account Entity
                    child = ChildEntity(
                    key_name=string2, 
                    parent=prt, 
                    str1=string1, 
                    str2_key=string2,
                    str3=string3,)
                child.put()
                return child
            db.run_in_transaction(txn)
        except Exception, e:
            raise e

class ChildEntity(db.Model):
    #foreign and primary key values
    str1 =      db.StringProperty()
    str2_key =  db.StringProperty()

    #pertinent data below
    str3 =      db.StringProperty()
+3
1

, , . , InsertData. txn(). , , , , .

, txn(), . !

+2

All Articles