Relationships and Joins

In this document we’ll cover how Peewee handles relationships between models.

Model definitions

We’ll use the following model definitions for our examples:

  1. import datetime
  2. from peewee import *
  3. db = SqliteDatabase(':memory:')
  4. class BaseModel(Model):
  5. class Meta:
  6. database = db
  7. class User(BaseModel):
  8. username = TextField()
  9. class Tweet(BaseModel):
  10. content = TextField()
  11. timestamp = DateTimeField(default=datetime.datetime.now)
  12. user = ForeignKeyField(User, backref='tweets')
  13. class Favorite(BaseModel):
  14. user = ForeignKeyField(User, backref='favorites')
  15. tweet = ForeignKeyField(Tweet, backref='favorites')

Peewee uses ForeignKeyField to define foreign-key relationships between models. Every foreign-key field has an implied back-reference, which is exposed as a pre-filtered Select query using the provided backref attribute.

Creating test data

To follow along with the examples, let’s populate this database with some test data:

  1. def populate_test_data():
  2. db.create_tables([User, Tweet, Favorite])
  3. data = (
  4. ('huey', ('meow', 'hiss', 'purr')),
  5. ('mickey', ('woof', 'whine')),
  6. ('zaizee', ()))
  7. for username, tweets in data:
  8. user = User.create(username=username)
  9. for tweet in tweets:
  10. Tweet.create(user=user, content=tweet)
  11. # Populate a few favorites for our users, such that:
  12. favorite_data = (
  13. ('huey', ['whine']),
  14. ('mickey', ['purr']),
  15. ('zaizee', ['meow', 'purr']))
  16. for username, favorites in favorite_data:
  17. user = User.get(User.username == username)
  18. for content in favorites:
  19. tweet = Tweet.get(Tweet.content == content)
  20. Favorite.create(user=user, tweet=tweet)

This gives us the following:

UserTweetFavorited by
hueymeowzaizee
hueyhiss 
hueypurrmickey, zaizee
mickeywoof 
mickeywhinehuey

Attention

In the following examples we will be executing a number of queries. If you are unsure how many queries are being executed, you can add the following code, which will log all queries to the console:

  1. import logging
  2. logger = logging.getLogger('peewee')
  3. logger.addHandler(logging.StreamHandler())
  4. logger.setLevel(logging.DEBUG)

Note

In SQLite, foreign keys are not enabled by default. Most things, including the Peewee foreign-key API, will work fine, but ON DELETE behaviour will be ignored, even if you explicitly specify on_delete in your ForeignKeyField. In conjunction with the default AutoField behaviour (where deleted record IDs can be reused), this can lead to subtle bugs. To avoid problems, I recommend that you enable foreign-key constraints when using SQLite, by setting pragmas={'foreign_keys': 1} when you instantiate SqliteDatabase.

  1. # Ensure foreign-key constraints are enforced.
  2. db = SqliteDatabase('my_app.db', pragmas={'foreign_keys': 1})

Performing simple joins

As an exercise in learning how to perform joins with Peewee, let’s write a query to print out all the tweets by “huey”. To do this we’ll select from the Tweet model and join on the User model, so we can then filter on the User.username field:

  1. >>> query = Tweet.select().join(User).where(User.username == 'huey')
  2. >>> for tweet in query:
  3. ... print(tweet.content)
  4. ...
  5. meow
  6. hiss
  7. purr

Note

We did not have to explicitly specify the join predicate (the “ON” clause), because Peewee inferred from the models that when we joined from Tweet to User, we were joining on the Tweet.user foreign-key.

The following code is equivalent, but more explicit:

  1. query = (Tweet
  2. .select()
  3. .join(User, on=(Tweet.user == User.id))
  4. .where(User.username == 'huey'))

If we already had a reference to the User object for “huey”, we could use the User.tweets back-reference to list all of huey’s tweets:

  1. >>> huey = User.get(User.username == 'huey')
  2. >>> for tweet in huey.tweets:
  3. ... print(tweet.content)
  4. ...
  5. meow
  6. hiss
  7. purr

Taking a closer look at huey.tweets, we can see that it is just a simple pre-filtered SELECT query:

  1. >>> huey.tweets
  2. <peewee.ModelSelect at 0x7f0483931fd0>
  3. >>> huey.tweets.sql()
  4. ('SELECT "t1"."id", "t1"."content", "t1"."timestamp", "t1"."user_id"
  5. FROM "tweet" AS "t1" WHERE ("t1"."user_id" = ?)', [1])

Joining multiple tables

Let’s take another look at joins by querying the list of users and getting the count of how many tweet’s they’ve authored that were favorited. This will require us to join twice: from user to tweet, and from tweet to favorite. We’ll add the additional requirement that users should be included who have not created any tweets, as well as users whose tweets have not been favorited. The query, expressed in SQL, would be:

  1. SELECT user.username, COUNT(favorite.id)
  2. FROM user
  3. LEFT OUTER JOIN tweet ON tweet.user_id = user.id
  4. LEFT OUTER JOIN favorite ON favorite.tweet_id = tweet.id
  5. GROUP BY user.username

Note

In the above query both joins are LEFT OUTER, since a user may not have any tweets or, if they have tweets, none of them may have been favorited.

Peewee has a concept of a join context, meaning that whenever we call the join() method, we are implicitly joining on the previously-joined model (or if this is the first call, the model we are selecting from). Since we are joining straight through, from user to tweet, then from tweet to favorite, we can simply write:

  1. query = (User
  2. .select(User.username, fn.COUNT(Favorite.id).alias('count'))
  3. .join(Tweet, JOIN.LEFT_OUTER) # Joins user -> tweet.
  4. .join(Favorite, JOIN.LEFT_OUTER) # Joins tweet -> favorite.
  5. .group_by(User.username))

Iterating over the results:

  1. >>> for user in query:
  2. ... print(user.username, user.count)
  3. ...
  4. huey 3
  5. mickey 1
  6. zaizee 0

For a more complicated example involving multiple joins and switching join contexts, let’s find all the tweets by Huey and the number of times they’ve been favorited. To do this we’ll need to perform two joins and we’ll also use an aggregate function to calculate the favorite count.

Here is how we would write this query in SQL:

  1. SELECT tweet.content, COUNT(favorite.id)
  2. FROM tweet
  3. INNER JOIN user ON tweet.user_id = user.id
  4. LEFT OUTER JOIN favorite ON favorite.tweet_id = tweet.id
  5. WHERE user.username = 'huey'
  6. GROUP BY tweet.content;

Note

We use a LEFT OUTER join from tweet to favorite since a tweet may not have any favorites, yet we still wish to display it’s content (along with a count of zero) in the result set.

With Peewee, the resulting Python code looks very similar to what we would write in SQL:

  1. query = (Tweet
  2. .select(Tweet.content, fn.COUNT(Favorite.id).alias('count'))
  3. .join(User) # Join from tweet -> user.
  4. .switch(Tweet) # Move "join context" back to tweet.
  5. .join(Favorite, JOIN.LEFT_OUTER) # Join from tweet -> favorite.
  6. .where(User.username == 'huey')
  7. .group_by(Tweet.content))

Note the call to switch() - that instructs Peewee to set the join context back to Tweet. If we had omitted the explicit call to switch, Peewee would have used User (the last model we joined) as the join context and constructed the join from User to Favorite using the Favorite.user foreign-key, which would have given us incorrect results.

If we wanted to omit the join-context switching we could instead use the join_from() method. The following query is equivalent to the previous one:

  1. query = (Tweet
  2. .select(Tweet.content, fn.COUNT(Favorite.id).alias('count'))
  3. .join_from(Tweet, User) # Join tweet -> user.
  4. .join_from(Tweet, Favorite, JOIN.LEFT_OUTER) # Join tweet -> favorite.
  5. .where(User.username == 'huey')
  6. .group_by(Tweet.content))

We can iterate over the results of the above query to print the tweet’s content and the favorite count:

  1. >>> for tweet in query:
  2. ... print('%s favorited %d times' % (tweet.content, tweet.count))
  3. ...
  4. meow favorited 1 times
  5. hiss favorited 0 times
  6. purr favorited 2 times

Selecting from multiple sources

If we wished to list all the tweets in the database, along with the username of their author, you might try writing this:

  1. >>> for tweet in Tweet.select():
  2. ... print(tweet.user.username, '->', tweet.content)
  3. ...
  4. huey -> meow
  5. huey -> hiss
  6. huey -> purr
  7. mickey -> woof
  8. mickey -> whine

There is a big problem with the above loop: it executes an additional query for every tweet to look up the tweet.user foreign-key. For our small table the performance penalty isn’t obvious, but we would find the delays grew as the number of rows increased.

If you’re familiar with SQL, you might remember that it’s possible to SELECT from multiple tables, allowing us to get the tweet content and the username in a single query:

  1. SELECT tweet.content, user.username
  2. FROM tweet
  3. INNER JOIN user ON tweet.user_id = user.id;

Peewee makes this quite easy. In fact, we only need to modify our query a little bit. We tell Peewee we wish to select Tweet.content as well as the User.username field, then we include a join from tweet to user. To make it a bit more obvious that it’s doing the correct thing, we can ask Peewee to return the rows as dictionaries.

  1. >>> for row in Tweet.select(Tweet.content, User.username).join(User).dicts():
  2. ... print(row)
  3. ...
  4. {'content': 'meow', 'username': 'huey'}
  5. {'content': 'hiss', 'username': 'huey'}
  6. {'content': 'purr', 'username': 'huey'}
  7. {'content': 'woof', 'username': 'mickey'}
  8. {'content': 'whine', 'username': 'mickey'}

Now we’ll leave off the call to “.dicts()” and return the rows as Tweet objects. Notice that Peewee assigns the username value to tweet.user.username – NOT tweet.username! Because there is a foreign-key from tweet to user, and we have selected fields from both models, Peewee will reconstruct the model-graph for us:

  1. >>> for tweet in Tweet.select(Tweet.content, User.username).join(User):
  2. ... print(tweet.user.username, '->', tweet.content)
  3. ...
  4. huey -> meow
  5. huey -> hiss
  6. huey -> purr
  7. mickey -> woof
  8. mickey -> whine

If we wish to, we can control where Peewee puts the joined User instance in the above query, by specifying an attr in the join() method:

  1. >>> query = Tweet.select(Tweet.content, User.username).join(User, attr='author')
  2. >>> for tweet in query:
  3. ... print(tweet.author.username, '->', tweet.content)
  4. ...
  5. huey -> meow
  6. huey -> hiss
  7. huey -> purr
  8. mickey -> woof
  9. mickey -> whine

Conversely, if we simply wish all attributes we select to be attributes of the Tweet instance, we can add a call to objects() at the end of our query (similar to how we called dicts()):

  1. >>> for tweet in query.objects():
  2. ... print(tweet.username, '->', tweet.content)
  3. ...
  4. huey -> meow
  5. (etc)

More complex example

As a more complex example, in this query, we will write a single query that selects all the favorites, along with the user who created the favorite, the tweet that was favorited, and that tweet’s author.

In SQL we would write:

  1. SELECT owner.username, tweet.content, author.username AS author
  2. FROM favorite
  3. INNER JOIN user AS owner ON (favorite.user_id = owner.id)
  4. INNER JOIN tweet ON (favorite.tweet_id = tweet.id)
  5. INNER JOIN user AS author ON (tweet.user_id = author.id);

Note that we are selecting from the user table twice - once in the context of the user who created the favorite, and again as the author of the tweet.

With Peewee, we use Model.alias() to alias a model class so it can be referenced twice in a single query:

  1. Owner = User.alias()
  2. query = (Favorite
  3. .select(Favorite, Tweet.content, User.username, Owner.username)
  4. .join(Owner) # Join favorite -> user (owner of favorite).
  5. .switch(Favorite)
  6. .join(Tweet) # Join favorite -> tweet
  7. .join(User)) # Join tweet -> user

We can iterate over the results and access the joined values in the following way. Note how Peewee has resolved the fields from the various models we selected and reconstructed the model graph:

  1. >>> for fav in query:
  2. ... print(fav.user.username, 'liked', fav.tweet.content, 'by', fav.tweet.user.username)
  3. ...
  4. huey liked whine by mickey
  5. mickey liked purr by huey
  6. zaizee liked meow by huey
  7. zaizee liked purr by huey

Subqueries

Peewee allows you to join on any table-like object, including subqueries or common table expressions (CTEs). To demonstrate joining on a subquery, let’s query for all users and their latest tweet.

Here is the SQL:

  1. SELECT tweet.*, user.*
  2. FROM tweet
  3. INNER JOIN (
  4. SELECT latest.user_id, MAX(latest.timestamp) AS max_ts
  5. FROM tweet AS latest
  6. GROUP BY latest.user_id) AS latest_query
  7. ON ((tweet.user_id = latest_query.user_id) AND (tweet.timestamp = latest_query.max_ts))
  8. INNER JOIN user ON (tweet.user_id = user.id)

We’ll do this by creating a subquery which selects each user and the timestamp of their latest tweet. Then we can query the tweets table in the outer query and join on the user and timestamp combination from the subquery.

  1. # Define our subquery first. We'll use an alias of the Tweet model, since
  2. # we will be querying from the Tweet model directly in the outer query.
  3. Latest = Tweet.alias()
  4. latest_query = (Latest
  5. .select(Latest.user, fn.MAX(Latest.timestamp).alias('max_ts'))
  6. .group_by(Latest.user)
  7. .alias('latest_query'))
  8. # Our join predicate will ensure that we match tweets based on their
  9. # timestamp *and* user_id.
  10. predicate = ((Tweet.user == latest_query.c.user_id) &
  11. (Tweet.timestamp == latest_query.c.max_ts))
  12. # We put it all together, querying from tweet and joining on the subquery
  13. # using the above predicate.
  14. query = (Tweet
  15. .select(Tweet, User) # Select all columns from tweet and user.
  16. .join(latest_query, on=predicate) # Join tweet -> subquery.
  17. .join_from(Tweet, User)) # Join from tweet -> user.

Iterating over the query, we can see each user and their latest tweet.

  1. >>> for tweet in query:
  2. ... print(tweet.user.username, '->', tweet.content)
  3. ...
  4. huey -> purr
  5. mickey -> whine

There are a couple things you may not have seen before in the code we used to create the query in this section:

  • We used join_from() to explicitly specify the join context. We wrote .join_from(Tweet, User), which is equivalent to .switch(Tweet).join(User).
  • We referenced columns in the subquery using the magic .c attribute, for example latest_query.c.max_ts. The .c attribute is used to dynamically create column references.
  • Instead of passing individual fields to Tweet.select(), we passed the Tweet and User models. This is shorthand for selecting all fields on the given model.

Common-table Expressions

In the previous section we joined on a subquery, but we could just as easily have used a common-table expression (CTE). We will repeat the same query as before, listing users and their latest tweets, but this time we will do it using a CTE.

Here is the SQL:

  1. WITH latest AS (
  2. SELECT user_id, MAX(timestamp) AS max_ts
  3. FROM tweet
  4. GROUP BY user_id)
  5. SELECT tweet.*, user.*
  6. FROM tweet
  7. INNER JOIN latest
  8. ON ((latest.user_id = tweet.user_id) AND (latest.max_ts = tweet.timestamp))
  9. INNER JOIN user
  10. ON (tweet.user_id = user.id)

This example looks very similar to the previous example with the subquery:

  1. # Define our CTE first. We'll use an alias of the Tweet model, since
  2. # we will be querying from the Tweet model directly in the main query.
  3. Latest = Tweet.alias()
  4. cte = (Latest
  5. .select(Latest.user, fn.MAX(Latest.timestamp).alias('max_ts'))
  6. .group_by(Latest.user)
  7. .cte('latest'))
  8. # Our join predicate will ensure that we match tweets based on their
  9. # timestamp *and* user_id.
  10. predicate = ((Tweet.user == cte.c.user_id) &
  11. (Tweet.timestamp == cte.c.max_ts))
  12. # We put it all together, querying from tweet and joining on the CTE
  13. # using the above predicate.
  14. query = (Tweet
  15. .select(Tweet, User) # Select all columns from tweet and user.
  16. .join(cte, on=predicate) # Join tweet -> CTE.
  17. .join_from(Tweet, User) # Join from tweet -> user.
  18. .with_cte(cte))

We can iterate over the result-set, which consists of the latest tweets for each user:

  1. >>> for tweet in query:
  2. ... print(tweet.user.username, '->', tweet.content)
  3. ...
  4. huey -> purr
  5. mickey -> whine

Note

For more information about using CTEs, including information on writing recursive CTEs, see the Common Table Expressions section of the “Querying” document.

Multiple foreign-keys to the same Model

When there are multiple foreign keys to the same model, it is good practice to explicitly specify which field you are joining on.

Referring back to the example app’s models, consider the Relationship model, which is used to denote when one user follows another. Here is the model definition:

  1. class Relationship(BaseModel):
  2. from_user = ForeignKeyField(User, backref='relationships')
  3. to_user = ForeignKeyField(User, backref='related_to')
  4. class Meta:
  5. indexes = (
  6. # Specify a unique multi-column index on from/to-user.
  7. (('from_user', 'to_user'), True),
  8. )

Since there are two foreign keys to User, we should always specify which field we are using in a join.

For example, to determine which users I am following, I would write:

  1. (User
  2. .select()
  3. .join(Relationship, on=Relationship.to_user)
  4. .where(Relationship.from_user == charlie))

On the other hand, if I wanted to determine which users are following me, I would instead join on the from_user column and filter on the relationship’s to_user:

  1. (User
  2. .select()
  3. .join(Relationship, on=Relationship.from_user)
  4. .where(Relationship.to_user == charlie))

Joining on arbitrary fields

If a foreign key does not exist between two tables you can still perform a join, but you must manually specify the join predicate.

In the following example, there is no explicit foreign-key between User and ActivityLog, but there is an implied relationship between the ActivityLog.object_id field and User.id. Rather than joining on a specific Field, we will join using an Expression.

  1. user_log = (User
  2. .select(User, ActivityLog)
  3. .join(ActivityLog, on=(User.id == ActivityLog.object_id), attr='log')
  4. .where(
  5. (ActivityLog.activity_type == 'user_activity') &
  6. (User.username == 'charlie')))
  7. for user in user_log:
  8. print(user.username, user.log.description)
  9. #### Print something like ####
  10. charlie logged in
  11. charlie posted a tweet
  12. charlie retweeted
  13. charlie posted a tweet
  14. charlie logged out

Note

Recall that we can control the attribute Peewee will assign the joined instance to by specifying the attr parameter in the join() method. In the previous example, we used the following join:

  1. join(ActivityLog, on=(User.id == ActivityLog.object_id), attr='log')

Then when iterating over the query, we were able to directly access the joined ActivityLog without incurring an additional query:

  1. for user in user_log:
  2. print(user.username, user.log.description)

Self-joins

Peewee supports constructing queries containing a self-join.

Using model aliases

To join on the same model (table) twice, it is necessary to create a model alias to represent the second instance of the table in a query. Consider the following model:

  1. class Category(Model):
  2. name = CharField()
  3. parent = ForeignKeyField('self', backref='children')

What if we wanted to query all categories whose parent category is Electronics. One way would be to perform a self-join:

  1. Parent = Category.alias()
  2. query = (Category
  3. .select()
  4. .join(Parent, on=(Category.parent == Parent.id))
  5. .where(Parent.name == 'Electronics'))

When performing a join that uses a ModelAlias, it is necessary to specify the join condition using the on keyword argument. In this case we are joining the category with its parent category.

Using subqueries

Another less common approach involves the use of subqueries. Here is another way we might construct a query to get all the categories whose parent category is Electronics using a subquery:

  1. Parent = Category.alias()
  2. join_query = Parent.select().where(Parent.name == 'Electronics')
  3. # Subqueries used as JOINs need to have an alias.
  4. join_query = join_query.alias('jq')
  5. query = (Category
  6. .select()
  7. .join(join_query, on=(Category.parent == join_query.c.id)))

This will generate the following SQL query:

  1. SELECT t1."id", t1."name", t1."parent_id"
  2. FROM "category" AS t1
  3. INNER JOIN (
  4. SELECT t2."id"
  5. FROM "category" AS t2
  6. WHERE (t2."name" = ?)) AS jq ON (t1."parent_id" = "jq"."id")

To access the id value from the subquery, we use the .c magic lookup which will generate the appropriate SQL expression:

  1. Category.parent == join_query.c.id
  2. # Becomes: (t1."parent_id" = "jq"."id")

Implementing Many to Many

Peewee provides a field for representing many-to-many relationships, much like Django does. This feature was added due to many requests from users, but I strongly advocate against using it, since it conflates the idea of a field with a junction table and hidden joins. It’s just a nasty hack to provide convenient accessors.

To implement many-to-many correctly with peewee, you will therefore create the intermediary table yourself and query through it:

  1. class Student(Model):
  2. name = CharField()
  3. class Course(Model):
  4. name = CharField()
  5. class StudentCourse(Model):
  6. student = ForeignKeyField(Student)
  7. course = ForeignKeyField(Course)

To query, let’s say we want to find students who are enrolled in math class:

  1. query = (Student
  2. .select()
  3. .join(StudentCourse)
  4. .join(Course)
  5. .where(Course.name == 'math'))
  6. for student in query:
  7. print(student.name)

To query what classes a given student is enrolled in:

  1. courses = (Course
  2. .select()
  3. .join(StudentCourse)
  4. .join(Student)
  5. .where(Student.name == 'da vinci'))
  6. for course in courses:
  7. print(course.name)

To efficiently iterate over a many-to-many relation, i.e., list all students and their respective courses, we will query the through model StudentCourse and precompute the Student and Course:

  1. query = (StudentCourse
  2. .select(StudentCourse, Student, Course)
  3. .join(Course)
  4. .switch(StudentCourse)
  5. .join(Student)
  6. .order_by(Student.name))

To print a list of students and their courses you might do the following:

  1. for student_course in query:
  2. print(student_course.student.name, '->', student_course.course.name)

Since we selected all fields from Student and Course in the select clause of the query, these foreign key traversals are “free” and we’ve done the whole iteration with just 1 query.

ManyToManyField

The ManyToManyField provides a field-like API over many-to-many fields. For all but the simplest many-to-many situations, you’re better off using the standard peewee APIs. But, if your models are very simple and your querying needs are not very complex, ManyToManyField may work.

Modeling students and courses using ManyToManyField:

  1. from peewee import *
  2. db = SqliteDatabase('school.db')
  3. class BaseModel(Model):
  4. class Meta:
  5. database = db
  6. class Student(BaseModel):
  7. name = CharField()
  8. class Course(BaseModel):
  9. name = CharField()
  10. students = ManyToManyField(Student, backref='courses')
  11. StudentCourse = Course.students.get_through_model()
  12. db.create_tables([
  13. Student,
  14. Course,
  15. StudentCourse])
  16. # Get all classes that "huey" is enrolled in:
  17. huey = Student.get(Student.name == 'Huey')
  18. for course in huey.courses.order_by(Course.name):
  19. print(course.name)
  20. # Get all students in "English 101":
  21. engl_101 = Course.get(Course.name == 'English 101')
  22. for student in engl_101.students:
  23. print(student.name)
  24. # When adding objects to a many-to-many relationship, we can pass
  25. # in either a single model instance, a list of models, or even a
  26. # query of models:
  27. huey.courses.add(Course.select().where(Course.name.contains('English')))
  28. engl_101.students.add(Student.get(Student.name == 'Mickey'))
  29. engl_101.students.add([
  30. Student.get(Student.name == 'Charlie'),
  31. Student.get(Student.name == 'Zaizee')])
  32. # The same rules apply for removing items from a many-to-many:
  33. huey.courses.remove(Course.select().where(Course.name.startswith('CS')))
  34. engl_101.students.remove(huey)
  35. # Calling .clear() will remove all associated objects:
  36. cs_150.students.clear()

Attention

Before many-to-many relationships can be added, the objects being referenced will need to be saved first. In order to create relationships in the many-to-many through table, Peewee needs to know the primary keys of the models being referenced.

Warning

It is strongly recommended that you do not attempt to subclass models containing ManyToManyField instances.

A ManyToManyField, despite its name, is not a field in the usual sense. Instead of being a column on a table, the many-to-many field covers the fact that behind-the-scenes there’s actually a separate table with two foreign-key pointers (the through table).

Therefore, when a subclass is created that inherits a many-to-many field, what actually needs to be inherited is the through table. Because of the potential for subtle bugs, Peewee does not attempt to automatically subclass the through model and modify its foreign-key pointers. As a result, many-to-many fields typically will not work with inheritance.

For more examples, see:

Avoiding the N+1 problem

The N+1 problem refers to a situation where an application performs a query, then for each row of the result set, the application performs at least one other query (another way to conceptualize this is as a nested loop). In many cases, these n queries can be avoided through the use of a SQL join or subquery. The database itself may do a nested loop, but it will usually be more performant than doing n queries in your application code, which involves latency communicating with the database and may not take advantage of indices or other optimizations employed by the database when joining or executing a subquery.

Peewee provides several APIs for mitigating N+1 query behavior. Recollecting the models used throughout this document, User and Tweet, this section will try to outline some common N+1 scenarios, and how peewee can help you avoid them.

Attention

In some cases, N+1 queries will not result in a significant or measurable performance hit. It all depends on the data you are querying, the database you are using, and the latency involved in executing queries and retrieving results. As always when making optimizations, profile before and after to ensure the changes do what you expect them to.

List recent tweets

The twitter timeline displays a list of tweets from multiple users. In addition to the tweet’s content, the username of the tweet’s author is also displayed. The N+1 scenario here would be:

  1. Fetch the 10 most recent tweets.
  2. For each tweet, select the author (10 queries).

By selecting both tables and using a join, peewee makes it possible to accomplish this in a single query:

  1. query = (Tweet
  2. .select(Tweet, User) # Note that we are selecting both models.
  3. .join(User) # Use an INNER join because every tweet has an author.
  4. .order_by(Tweet.id.desc()) # Get the most recent tweets.
  5. .limit(10))
  6. for tweet in query:
  7. print(tweet.user.username, '-', tweet.message)

Without the join, accessing tweet.user.username would trigger a query to resolve the foreign key tweet.user and retrieve the associated user. But since we have selected and joined on User, peewee will automatically resolve the foreign-key for us.

Note

This technique is discussed in more detail in Selecting from multiple sources.

List users and all their tweets

Let’s say you want to build a page that shows several users and all of their tweets. The N+1 scenario would be:

  1. Fetch some users.
  2. For each user, fetch their tweets.

This situation is similar to the previous example, but there is one important difference: when we selected tweets, they only have a single associated user, so we could directly assign the foreign key. The reverse is not true, however, as one user may have any number of tweets (or none at all).

Peewee provides an approach to avoiding O(n) queries in this situation. Fetch users first, then fetch all the tweets associated with those users. Once peewee has the big list of tweets, it will assign them out, matching them with the appropriate user. This method is usually faster but will involve a query for each table being selected.

Using prefetch

peewee supports pre-fetching related data using sub-queries. This method requires the use of a special API, prefetch(). Prefetch, as its name implies, will eagerly load the appropriate tweets for the given users using subqueries. This means instead of O(n) queries for n rows, we will do O(k) queries for k tables.

Here is an example of how we might fetch several users and any tweets they created within the past week.

  1. week_ago = datetime.date.today() - datetime.timedelta(days=7)
  2. users = User.select()
  3. tweets = (Tweet
  4. .select()
  5. .where(Tweet.timestamp >= week_ago))
  6. # This will perform two queries.
  7. users_with_tweets = prefetch(users, tweets)
  8. for user in users_with_tweets:
  9. print(user.username)
  10. for tweet in user.tweets:
  11. print(' ', tweet.message)

Note

Note that neither the User query, nor the Tweet query contained a JOIN clause. When using prefetch() you do not need to specify the join.

prefetch() can be used to query an arbitrary number of tables. Check the API documentation for more examples.

Some things to consider when using prefetch():

  • Foreign keys must exist between the models being prefetched.
  • LIMIT works as you’d expect on the outer-most query, but may be difficult to implement correctly if trying to limit the size of the sub-selects.