python - GAE——祖先问题

标签 python database google-app-engine transactional

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

我得到了同样的错误

BadRequestError:

Only ancestor queries are allowed inside transactions.

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

最佳答案

避免此问题的方法是使用用户名作为 key 。

@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

关于python - GAE——祖先问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11386086/

相关文章:

python - Panda 对数据框的数值操作(即 max())

database - 每个客户使用一个大型数据库或使用单个数据库

google-app-engine - 如何使用 Play Framework 和 Google App Engine 创建 PDF?

java - Google App Engine 上的 ServletContext 类路径中的 "context"在哪里?

python - 当两个数据帧的索引不同时,将一个 pandas 数据帧中的一列添加到另一个数据帧中

python - 使用python游标逐行加载数据

python - 从 Alpine Linux Docker 连接到 MS SQL 时遇到问题

database - 应该使用什么数据库/技术来计算时间范围内的唯一身份访问者

database - 各大数据库 "GenerationType.AUTO"究竟选择了什么策略?

google-app-engine - 获取所有记录时,Google Cloud Datastore 查询速度太慢