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 a开发者_运维百科nd 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()
@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()
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!
精彩评论