From fe039e4092081ffe6ff78518acb5a82e9518e310 Mon Sep 17 00:00:00 2001 From: Lephe Date: Wed, 7 Jul 2021 19:22:36 +0200 Subject: [PATCH] forum: fix a bug in c59e84485 for forums with no topics WHERE would be on its own without argument. --- app/routes/forum/index.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) 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,