RLH RLH - 1 year ago 47
Python Question

Inserting two enties in a transaction & getting 'Cannot operate on different entity groups in a transaction' Error

My end goal is simple. I need to have an entity that has two, unique, indexed fields that can operate like keys. If this was a SQL database, the equivelant would be having two fields that are defined as both unique and are independant of one another. I know this functionality isn't directly possible for one data store db.Model, so I've had to create a parent-child Model scenario that mimics this behavior.

To solve this problem, I've created two Models (ParentEntity and ChildEntity.) The ParentEntity model is a dummy db.Model that stores the two values of the two keys but only one of the keys is assigned to the key_name parameter of Model #1.

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

My assumption is that this would keep these entities within the same entity group because that is what the google documentation implies.

I've added an insertion method named InsertData to the ParentEntity (which could just as easily be placed in the ChildEntity) which I can call to control this insertion logic and attempts to insert these records via a transaction.

When I call InsertData I get the follow error:

Cannot operate on different entity
groups in a transaction: (kind='ChildEntity', name='key_name > 2') and (kind='ParentEntity', name='key_name 1').

If my second (ChildEntity) entity is assigned the first entity (ParentEntity) to the parent parameter, shouldn't these two entities be in the same entity group?

The code provided is a functional copy of what I am trying to achieve. The only difference is that a few extra properties are stored in ChildEntity, a bit of data validation takes place before txn() definition and I've changed the names of the fields to more meaningful names for this question.

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

def InsertData(string1, string2, string3):
def txn():
#create first entity
prt = ParentEntity(

#create User Account Entity
child = ChildEntity(
return child
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()

Answer Source

I've fixed this problem but it the solution is unrelated to the setup mentioned above. As I had previously stated, my actual class contains some validation code within the InsertData method. Portions of the Validation logic were taking place in the txn() method. I assumed that this wouldn't be a problem because all my validation does is check to be sure that there are text values within certain parameters and that one specific parameter is of a certain length.

After I moved the validation from the txn() method, the insertion operation has worked without a problem. Excellent!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download