Google App Engine数据存储区中的多对多关系(ndb) [英] Many-To-Many Relationships in Google App Engine Datastore (ndb)

查看:90
本文介绍了Google App Engine数据存储区中的多对多关系(ndb)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种模式:会员和活动。一个成员可以参加许多活动,并且在一个活动中,有许多参与者。我想是这样的:

$ p $ 类成员(ndb.model):
event = ndb.KeyProperty(重复= True)

class Events(ndb.model):
member = ndb.KeyProperty(repeated = True)

执行多对多关系的最佳方法是什么?

解决方案

认为在这种情况下,你想在一个模型中有一个指向另一个模型的数组/键列表。由于数组/列表的长度有限制(现在最大为5000),您可能希望让成员模型指向事件模型(我假设成员可能不会去很多事件(超过5000),但在一个事件中有很多(超过5000)成员)。现在,如果您想要在成员模型中查询事件关键字的与会者列表。如果您只是想要计算头部数量,您可以做一个关键的查询,这将节省成本。



如果由于某种原因,您需要两种情况都超过5000,那么您可以制作另一个(与会者)模型,其中包含会员密钥和事件密钥,然后针对每个事件为每位来宾制作此与会者模型。要获得客人名单只需查询特定事件的此模型,如果您需要成员的事件列表,则相反。这不是比以上更多的工作,所以即使5000不是一个限制因素,也可能是你的做法。


I have two models: Members and Events. A member can participe in many events and in an event, there are many participants. I think about like this:

class Members(ndb.model):
    event = ndb.KeyProperty(repeated=True)

class Events(ndb.model):
    member = ndb.KeyProperty(repeated=True)

What's the best way to do many-to-many relationship?

解决方案

I think in this case you want to have an array/list of keys in one model pointing to the other model. Since there is a limit on the length of the array/list (the max is 5000 right now) you probably want to have the member model point to the events models (I am assuming members probably don't go to many events (more than 5000), but there are many (more than 5000) members at an event). Now if you want the list of attendees you just query for the event key in the members models. You can do a key only query if you just want the head count, this will save on cost.

If for some reason you need to have both cases be over 5000, then you can make another (attendee) model that contains a member key and an event key, then make this attendee model for each guest for each event. To get the list of guests just query this model for a specific event, if you need a list of events for a member do the opposite. This isn't much more work than the above so might be your go to way of doing it even if 5000 isn't a limiting factor.

这篇关于Google App Engine数据存储区中的多对多关系(ndb)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆