【问题标题】:GAE Datastore IDGAE 数据存储 ID
【发布时间】:2012-11-22 22:51:13
【问题描述】:

我创建了两个不同的实体,一个是用户,一个是他们可以创建的消息。我为每个用户分配了一个 ID,然后想将此 ID 分配给该用户创建的每条消息。我该怎么办?我必须在查询中这样做吗?

谢谢

【问题讨论】:

    标签: google-app-engine google-cloud-datastore


    【解决方案1】:

    假设您使用的是 Python NDB,您可以使用以下内容:

    class User(ndb.Model):
        # put your fileds here
    
    class Message(ndb.Model):
        owner = ndb.KeyProperty()
        # other fields
    

    创建并保存用户:

    user = User(field1=value1, ....)
    user.put()
    

    创建并保存消息:

    message = Message(owner=user.key, ...)
    message.put()
    

    根据用户查询消息:

    messages = Message.query().filter(Message.owner==user.key).fetch() # returns a list of messages that have this owner
    

    有关 NDB 的更多信息,请查看Python NDB API

    另外,您应该查看Python Datastore,以便更好地了解 App Engine 中的数据建模。

    【讨论】:

    • 如前所述创建实体祖先组时,请确保为该组处理每秒一次写入的约束。因此,如果用户一次收到 5 条消息,则需要进行一些异常处理。
    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 2023-04-02
    • 1970-01-01
    • 2013-04-07
    相关资源
    最近更新 更多