diff --git a/app/routes/forum/index.py b/app/routes/forum/index.py index 023b881..d843872 100644 --- a/app/routes/forum/index.py +++ b/app/routes/forum/index.py @@ -83,12 +83,13 @@ def forum_page(f, page=1): # Count comments; this direct request avoids performing one request for # each topic.thread.comments.count() in the view, which the database # doesn't really appreciate performance-wise. - req_comment_counts = \ - "SELECT thread_id, COUNT(*) FROM comment WHERE " + \ - " OR ".join(f"thread_id={t.thread.id}" for t in topics.items) + \ - " GROUP BY thread_id" + selection = " OR ".join(f"thread_id={t.thread.id}" for t in topics.items) + selection = "WHERE " + selection if selection else "" - comment_counts = db.session.execute(req_comment_counts) + comment_counts = db.session.execute(f""" + SELECT thread_id, COUNT(*) FROM comment {selection} + GROUP BY thread_id + """) comment_counts = dict(list(comment_counts)) return render('/forum/forum.html', f=f, topics=topics, form=form,