开发者

Google AppEngine entity groups and transactions

I'm trying to get a transaction to work in AppEngine and I'm running into problems with entity groups. My code is开发者_开发知识库 a bit like this:

parent_obj = ClassA.all().get()

def txn():
  key_name = 'hash of something here'

  if not db.get(db.Key.from_path('ClassB', key_name, parent=parent_obj)):
    obj = ClassB(
      parent = parent_obj
    )
    obj.put()

db.run_in_transaction(txn)

...but I get the 'Cannot operate on different entity groups in a transaction' error. What I don't understand is that as far as I can see my transaction only operates on entities in the same group. Namely, line 6 queries with a 'parent' which is the same as the 'parent' which is set in line 8, so both queries are concerned with the same entity group.

I'm left to conclude that my understanding of entity groups is wrong. But how? I've read the docs several times and still don't see how what I'm doing is wrong.

Any ideas? Thanks!


This is probably happening because parent_obj is None and you are not passing a key_name when creating ClassB. In this case, you have multiple entity groups (each entity with no ancestor is a separate group).

This would work if parent_obj is None or not:

parent_obj = ClassA.all().get()

def txn():
  key_name = 'hash of something here'

  if not db.get(db.Key.from_path('ClassB', key_name, parent=parent_obj)):
    obj = ClassB(
      key_name = key_name,
      parent = parent_obj
    )
    obj.put()

db.run_in_transaction(txn)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜