threads_q = c.forum.threads \
.join(forum_model.Thread.last_post) \
.order_by(forum_model.Post.posted_time.desc()) \
- .options(joinedload('last_post.author'))
+ .options(
+ joinedload('last_post'),
+ joinedload('last_post.author'),
+ )
c.num_threads = threads_q.count()
try:
c.skip = int(request.params.get('skip', 0))
Forum.threads = relation(Thread, order_by=Thread.id.desc(), lazy='dynamic', backref='forum')
Thread.posts = relation(Post, order_by=Post.position.asc(), lazy='dynamic', backref='thread')
-Thread.first_post = relation(Post, primaryjoin=and_(Post.thread_id == Thread.id, Post.position == 1), uselist=False)
+Thread.first_post = relation(Post, primaryjoin=and_(Post.thread_id == Thread.id, Post.position == 1), foreign_keys=[Thread.id], innerjoin=True, uselist=False)
# XXX THIS WILL NEED TO CHANGE when posts can be deleted! Or change what 'position' means
-Thread.last_post = relation(Post, primaryjoin=and_(Post.thread_id == Thread.id, Post.position == Thread.post_count), uselist=False)
+Thread.last_post = relation(Post, primaryjoin=and_(Post.thread_id == Thread.id, Post.position == Thread.post_count), foreign_keys=[Thread.id, Thread.post_count], innerjoin=True, uselist=False)
Post.author = relation(users_model.User, backref='posts')