Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Many-To-Many Relationships in Google App Engine Datastore (ndb)

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?

like image 996
tsil Avatar asked Nov 26 '12 12:11

tsil


1 Answers

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.

like image 87
Michael Avatar answered Sep 18 '22 08:09

Michael