ndb中的一对多关系

Hou*_*man 4 google-app-engine app-engine-ndb

我正在阅读Google应用引擎并准备样本以更好地理解它.

简而言之,用户可以记录该月中每天的条目,如日历.用户可以按月查看条目.所以一次不超过30小时.

最初我曾经使用过,db而且一对多的关系是直截了当的.

但是一旦我遇到了这个ndb,我意识到有两种方法可以建立一对多的关系.

1)结构化属性似乎就像User模型上的重复属性一样.这是否意味着如果我检索一个用户,我会自动检索她输入的所有记录?(例如整年)这不是很有效,不是吗?我想这样做的好处是你可以在一次读取操作中获得所有相关数据.

    from google.appengine.ext import ndb

    class User(UserMixin, ndb.Model):
        email = ndb.StringProperty(required = True)
        password_hash = ndb.TextProperty(required = True)
        record = ndb.StructuredProperty(Record, repeated=True)

    class Record(ndb.Model):            
        notes = ndb.TextProperty()
Run Code Online (Sandbox Code Playgroud)

2)或者我可以使用更经典的方式:

    class User(UserMixin, ndb.Model):
        email = ndb.StringProperty(required = True)
        password_hash = ndb.TextProperty(required = True)

    class Record(ndb.Model):
        user = ndb.KeyProperty(kind=User)
        notes = ndb.TextProperty()
Run Code Online (Sandbox Code Playgroud)

在我的用例中哪种方式更好?

Gui*_*sum 8

使用StructuredProperty而不是KeyProperty的缺点是,对于StructuredProperty,总实体大小(1MB)的限制适用于用户及其包含的所有记录的总和(因为结构化属性被序列化为用户实体的一部分).使用KeyProperty,每个记录本身都有1MB的限制.