python - Inserting two enties in a transaction & getting 'Cannot operate on different entity groups in a transaction' Error -
my end goal simple. need have entity has two, unique, indexed fields can operate keys. if sql database, equivelant having 2 fields defined both unique , independant of 1 another. know functionality isn't directly possible 1 data store db.model, i've had create parent-child model scenario mimics behavior.
to solve problem, i've created 2 models (parententity , childentity.) parententity model dummy db.model stores 2 values of 2 keys 1 of keys assigned key_name parameter of model #1.
after creating parent entity, create second, child entity assigning second key key_name , assigning parent entity created child entities parent parameter in constructor of new childentity object.
my assumption keep these entities within same entity group because google documentation implies.
i've added insertion method named insertdata parententity (which placed in childentity) can call control insertion logic , attempts insert these records via transaction.
when call insertdata follow error:
cannot operate on different entity groups in transaction: (kind='childentity', name='key_name > 2') , (kind='parententity', name='key_name 1').
if second (childentity) entity assigned first entity (parententity) parent parameter, shouldn't these 2 entities in same entity group?
the code provided functional copy of trying achieve. difference few properties stored in childentity, bit of data validation takes place before txn() definition , i've changed names of fields more meaningful names 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 , primary key values str1 = db.stringproperty() str2_key = db.stringproperty() #pertinent data below str3 = db.stringproperty()
i've fixed problem solution unrelated setup mentioned above. had stated, actual class contains validation code within insertdata method. portions of validation logic taking place in txn() method. assumed wouldn't problem because validation check sure there text values within parameters , 1 specific parameter of length.
after moved validation txn() method, insertion operation has worked without problem. excellent!
Comments
Post a Comment