【问题标题】:GAE - Ancestor issueGAE - 祖先问题
【发布时间】:2012-07-08 19:42:57
【问题描述】:

我想在我的 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

我经常遇到同样的错误

错误请求错误:

事务中只允许祖先查询。

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

【问题讨论】:

  • 您在事务中进行查询。您的查询没有ancestor。错误似乎是一致的
  • 所以我不允许在事务中进行查询?然后我认为我知道的唯一方法就是在内存缓存上放一些东西(我认为内存缓存也应该是原子的)
  • 我没有足够的知识来充分回答您的问题,但我的计算属性也有同样的问题:这会进行查询并返回一个简单的总和。我不得不删除所有交易并直接投入以继续开发。在您的情况下,请尝试不使用装饰器.. 我们会有更好的答案

标签: python database google-app-engine transactional


【解决方案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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多