From 6517fc9550dc93c3839a8d43a51956e3a44754b4 Mon Sep 17 00:00:00 2001 From: Untone Date: Wed, 7 Aug 2024 13:57:37 +0300 Subject: [PATCH] groupby --- resolvers/reader.py | 50 ++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/resolvers/reader.py b/resolvers/reader.py index 15de32a0..dcad7a8e 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -48,33 +48,33 @@ def query_shouts(): ).label("rating_stat"), func.max(aliased_reaction.created_at).label("last_reacted_at"), func.json_agg( - func.distinct( - func.json_build_object( - "id", - Author.id, - "name", - Author.name, - "slug", - Author.slug, - "pic", - Author.pic, - ) + func.json_build_object( + "id", + Author.id, + "name", + Author.name, + "slug", + Author.slug, + "pic", + Author.pic, ) - ).label("authors"), + ) + .filter(Author.id.is_not(None)) + .label("authors"), func.json_agg( - func.distinct( - func.json_build_object( - "id", - Topic.id, - "title", - Topic.title, - "body", - Topic.body, - "slug", - Topic.slug, - ) + func.json_build_object( + "id", + Topic.id, + "title", + Topic.title, + "body", + Topic.body, + "slug", + Topic.slug, ) - ).label("topics"), + ) + .filter(Topic.id.is_not(None)) + .label("topics"), ) .outerjoin(aliased_reaction, aliased_reaction.shout == Shout.id) .outerjoin(shout_author, shout_author.shout == Shout.id) @@ -82,7 +82,7 @@ def query_shouts(): .outerjoin(shout_topic, shout_topic.shout == Shout.id) .outerjoin(Topic, Topic.id == shout_topic.topic) .where(and_(Shout.published_at.is_not(None), Shout.deleted_at.is_(None))) - .group_by(Shout.id) + .group_by(Shout.id, Author.id, Topic.id) ) return q, aliased_reaction