2012-07-08 68 views
2

我想在我的gae中拥有独特的价值,所以我通过文档阅读并发现“交易”是原子的。GAE - 祖先问题

https://developers.google.com/appengine/docs/python/ndb/transactions

class Account(ndb.Model): 
    """"Required DB """ 
    username = ndb.StringProperty(required=True) 
    password = ndb.StringProperty(required=True) 
    mail = ndb.StringProperty(required=True) 
    salt = ndb.StringProperty(required=True) 
    date = ndb.DateTimeProperty(auto_now_add=True) 

    name = ndb.StringProperty() 
    last_name = ndb.StringProperty() 
    phone_number = ndb.IntegerProperty() 
    postal = ndb.IntegerProperty() 
    city = ndb.StringProperty() 

    products = ndb.IntegerProperty(repeated=True) 

    @ndb.transactional 
    def create_account(self): 
     acc = Account.query(Account.username==self.username) 
     acc = tuple(acc) 
     if len(acc)== 0: 
      self.put() 
     else: 
      #yield error 
      pass 

我awalys得到同样的错误

BadRequestError:

Only ancestor queries are allowed inside transactions.

我的数据库模型 “帐户” 没有任何祖先。 它不应该是唯一的“祖先”吗?

+0

您在事务中进行查询。你的查询没有'ancestor'。错误似乎是一致的 – 2012-07-08 20:03:17

+0

所以我不允许在事务内进行查询?然后,我认为我知道的唯一方法是将内容放在内存缓存中(我认为内存缓存也应该是原子的) – 2012-07-09 10:31:42

+0

我没有足够的知识来充分响应您的问题,但是我的计算属性存在相同的问题( s):这会进行查询并返回一个简单的总和。我不得不删除所有交易 并直接投入继续发展。在你的情况下尝试没有装饰者..我们将有更好的答案 – 2012-07-09 12:30:32

回答

1

解决此问题的一种方法是使用用户名作为密钥。

@classmethod 
@ndb.transactional 
def create(cls, username): 
    key = ndb.Key('Account', username) 
    existing_user = key.get() 
    if existing_user: 
     raise ValueError 
    else: 
     new_instance = cls(key=key, username=username) 
     new_instance.put() 
    return new_instance 
+0

谢谢。但你能向我解释一个关键是什么吗?键和查询有什么区别?我阅读https://developers.google.com/appengine/docs/python/ndb/entities,但我仍然困惑 – 2012-07-19 15:51:41

+0

请参阅https://developers.google.com/appengine/docs/python/ndb/keyclass,它是实体的数据库密钥。 – Chris 2013-01-01 18:54:28