ndb建模一对多:重复KeyProperty与外键的优点 [英] ndb modelling one-to-many: merits of repeated KeyProperty vs foreign key
问题描述
我的问题是关于在ndb中建模一对多关系。我知道这可以用(至少)两种不同的方式完成:重复属性或外键。我在下面创建了一个小例子。基本上我们有一篇文章可以有任意数量的标签。我们假设一个标签可以被删除,但添加后不能更改。我们还假设我们不担心事务安全性。
我的问题是:对这些关系进行建模的首选方式是什么?
我的考虑:
- 方法(A)需要为每个标签写两次被添加到
文章(一个用于文章,另一个用于标签),而
(B)的方法只需要一次写入(只是标签)。
- 方法(A)在获取文章的所有标签时利用
ndb的缓存机制,而在需要查询的情况下需要
(并且还需要一些
自定义缓存)
是否有一些我在这里遗漏的东西,应该考虑的其他因素?
非常感谢您的帮助。
示例(A):
$ b $文章(ndb.Model):
title = ndb.StringPropert y()
#更多属性
tags = ndb.KeyProperty(kind =Tag,repeated = True)
$ b $ def create_tag(self):
#需要两次写入
tag = Tag(name =my_tag)
tag.put()
self.tags.append(tag)
self.put()
def get_tags(self):
return ndb.get_multi(self.tags)
class标记(ndb.Model):
name = ndb.StringProperty( )
user = ndb.KeyProperty(Kind =User)#创建标记的用户
#更多属性
例子(B):
<$ c
$(需要)一个写
tag =标签(name =my_tag,article = self.key)
tag.put()
$ b $ get get_tags(self):
#显然我们可以缓存这个查询在memcache中
返回Tag.gql(WHERE文章:1,self.key)
类标记(ndb.Model):
name = ndb.StringProperty()
article = ndb.KeyProperty(kind =Article)
user = ndb.KeyProperty(Kind =User)#创建标记的用户
#更多属性
您是否看过以下关于使用结构化属性
https://developers.google.com/ appengine / docs / python / ndb / properties#structured 。关于联系人
和地址
的简短讨论可能会简化您的问题。另请参阅 https://developers.google.com/appengine/docs/蟒蛇/ NDB /查询#filtering_structured_properties 的。讨论非常简短。
此外,展望不允许连接的事实,选项 A
的样子更好。
My question is about modelling one-to-many relations in ndb. I understand that this can be done in (at least) two different ways: with a repeated property or with a 'foreign key'. I have created a small example below. Basically we have an Article which can have an arbitrary number of Tags. Let's assume that a Tag can be removed but cannot be changed after it has been added. Let's also assume that we don't worry about transactional safety.
My question is: what is the preferred way of modelling these relationships?
My considerations:
- Approach (A) requires two writes for every tag that is added to an article (one for the Article and one for the Tag) whereas approach (B) only requires one write (just the Tag).
- Approach (A) leverages ndb's caching mechanism when fetching all Tags for an Article whereas in case of approach (B) a query is required (and additionally some custom caching)
Are there some things that I'm missing here, any other considerations that should be taken into account?
Thanks very much for your help.
Example (A):
class Article(ndb.Model):
title = ndb.StringProperty()
# some more properties
tags = ndb.KeyProperty(kind="Tag", repeated=True)
def create_tag(self):
# requires two writes
tag = Tag(name="my_tag")
tag.put()
self.tags.append(tag)
self.put()
def get_tags(self):
return ndb.get_multi(self.tags)
class Tag(ndb.Model):
name = ndb.StringProperty()
user = ndb.KeyProperty(Kind="User") # User that created the tag
# some more properties
Example(B):
class Article(ndb.Model):
title = ndb.StringProperty()
# some more properties
def create_tag(self):
# requires one write
tag = Tag(name="my_tag", article=self.key)
tag.put()
def get_tags(self):
# obviously we could cache this query in memcache
return Tag.gql("WHERE article :1", self.key)
class Tag(ndb.Model):
name = ndb.StringProperty()
article = ndb.KeyProperty(kind="Article")
user = ndb.KeyProperty(Kind="User") # User that created the tag
# some more properties
Have you looked at the following about using Structured Properties
https://developers.google.com/appengine/docs/python/ndb/properties#structured . The short discussion there about Contact
and Addresse
may simplify your problem. Also look at https://developers.google.com/appengine/docs/python/ndb/queries#filtering_structured_properties. The discussions are very short.
Also, looking ahead to the fact that joins are not allowed, option A
looks better.
这篇关于ndb建模一对多:重复KeyProperty与外键的优点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!