While working on the site I found a need to join two tables together. I didn’t find many articles on how to do this, but did find the datastore has joins built in. The code below shows how to join a table using the ReferenceProperty. When you do this the record from the joined table becomes assessable via an object within the main record. This is a cool feature and makes database programing simple.
class Author(db.Model):
name = db.StringProperty()
class Story(db.Model):
author = db.ReferenceProperty(Author)
story = db.get(story_key)
author_name = story.author.name
Advertisement
Tags: Database, Datastore, Google app engine, table joins