App Engine Datastore Joins

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: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.