我正在阅读 Google 应用引擎并准备一个示例以更好地理解它。
简而言之,用户可以为一个月中的每一天记录一个条目,就像日历一样。
并且用户可以按月查看条目。所以一次不超过30 ish。
最初我使用 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()
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()
在我的用例中哪种方式更好?
最佳答案
使用 StructuredProperty 而不是 KeyProperty 的缺点是,对于 StructuredProperty,总实体大小 (1MB) 的限制适用于用户及其包含的所有记录的总和(因为结构化属性作为用户实体的一部分被序列化)。使用 KeyProperty,每个 Record 本身都有 1MB 的限制。