minarai
minarai

Reputation: 41

Query between three tables, many to many relationship

I have three databases in GAE. Hobby, Attendee and Event.

class Hobby(db.Model):
    name = db.StringProperty()


htest = Hobby.get_or_insert('tennis')
htest.name = 'tennis'
htest.put()
htest = Hobby.get_or_insert('basketball')
htest.name = 'basketball'
htest.put()
htest = Hobby.get_or_insert('food')

class Event(db.Model):
   title = db.StringProperty(required=True)
   description = db.TextProperty()
   time = db.DateTimeProperty()
   location = db.TextProperty()
   creator = db.UserProperty()
   edit_link = db.TextProperty()
   gcal_event_link = db.TextProperty()
   gcal_event_xml = db.TextProperty()
   hobby = db.ReferenceProperty(Hobby)

class Attendee(db.Model):
   email = db.StringProperty()
   hobbies = db.ListProperty(db.Key)
   event = db.ReferenceProperty(Event)

Each Attendee can pick however many hobby as they desire. When a event is created, user chooses a hobby to associate the event with and invitation will be send to every attendee who has chosen that hobby. Hobby DB is preloaded database.
I want to make a query that does that.

after reading Nick's blog http://blog.notdot.net/2010/10/Modeling-relationships-in-App-Engine which was very helpful I feel like I'm supposed to use the method that was mentioned in there

attendees = Attendee.all()filter('hobbies =', basketball).fetch(100)

however, i'm stuck there... any help would be really appreciated.

Upvotes: 1

Views: 139

Answers (1)

Louis
Louis

Reputation: 2890

I think you should record the invitations send in a table, say "invitationsSend" with two fields : event and attendee, which two fields are making a unique primary key.

To build this, you will have to select the data between both your tables event and attendees :

insert into invitationsSend(select E.Event, A.Attendee from Event as E, Attendee as A where E.Hobby = A.Hobby)

But I'm not familiar with your "db.listProperty" used for "hobbies" and I do not know how to look into that list. I should do this as a separate table with data "Attendee, Hobby", both as primary key.

Regards,

Upvotes: 1

Related Questions