在交易中插入两个物品&获取'无法在事务中的不同实体组上操作'错误 [英] Inserting two enties in a transaction & getting 'Cannot operate on different entity groups in a transaction' Error
问题描述
为了解决这个问题,我创建了两个模型(ParentEntity和ChildEntity)。ParentEntity模型是一个 dummy db.Model,它存储两个键的两个值,但只有一个键被分配到Model#1的 key_name 参数。
在创建父实体之后,我通过将第二个键分配为 key_name 并创建第二个子实体,并将其分配给父实体为新的ChildEntity对象的构造函数中的子实体 parent 参数创建。
我的假设是,这会将这些实体保留在同一个实体组中,因为这是Google文档所暗示的。
我向ParentEntity中添加了一个名为 InsertData 的插入方法(可以轻松地放入ChildEntity中),我可以调用它来控制这个插入逻辑并尝试通过事务插入这些记录。
当我调用 InsertData 时,出现以下错误:
$ b $ (kind ='ChildEntity',name ='key_name> 2')和(kind ='ParentEntity',name ='key_name 1')。无法对事务中的不同实体
组进行操作。 / p>
如果我的第二个(ChildEntity)实体被指派给父参数的第一个实体(ParentEntity),shouldn这两个实体是否在同一个实体组中?
所提供的代码是我尝试实现的功能副本。唯一的区别是一些额外的属性存储在ChildEntity中,一些数据验证发生在txn()定义之前,并且我已经将这些字段的名称更改为这个问题更有意义的名称。
class ParentEntity(db.Model):
str1_key = db.StringProperty()
str2 = db.StringProperty()
@staticmethod
Insert InsertData(string1,string2,string3):
try:
def txn():
#create first entity
prt = ParentEntity(
key_name = string1,
str1_key = string1,
str2 = string2)
prt.put()
#create用户帐户实体
child = ChildEntity(
key_name = string2,
parent = prt,
str1 = string1,
str2_key = string2,
$ b child.put()
返回子
db.run_in_transaction(txn)
除了异常,e:
提升e
$ b $ class ChildEntity(db.Model):
#foreign和主键值
str1 = db.StringProperty()
str2_key = db.StringProperty()
#pertinent data below
str3 = db.StringProperty()
我已经解决了这个问题,但它的解决方案与上面提到的设置无关。正如我之前所说的,我的实际类在InsertData方法中包含一些验证代码。验证逻辑的一部分发生在txn()方法中。我认为这不会是一个问题,因为我所做的所有验证都是检查以确保某些参数中有文本值,并且一个特定参数具有特定长度。
在我从txn()方法移动验证后,插入操作没有问题。非常好!
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()
@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!
这篇关于在交易中插入两个物品&获取'无法在事务中的不同实体组上操作'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!