diff --git a/resolvers/stat.py b/resolvers/stat.py index 713a72fd..7a5ed004 100644 --- a/resolvers/stat.py +++ b/resolvers/stat.py @@ -1,4 +1,4 @@ -from sqlalchemy import and_, distinct, func, join, select, CompoundSelect +from sqlalchemy import and_, distinct, func, join, select from sqlalchemy.orm import aliased from orm.author import Author, AuthorFollower @@ -181,12 +181,10 @@ def get_with_stat(q): records = [] try: with local_session() as session: - # Convert the CompoundSelect object to a Query object - if isinstance(q, CompoundSelect): - q = session.query().from_statement(q) - # detect author is_author = f"{q}".lower().startswith("select author") + + # Add stat columns to the query q = add_author_stat_columns(q) if is_author else add_topic_stat_columns(q) # execute query diff --git a/services/triggers.py b/services/triggers.py index 4101702c..8153bb81 100644 --- a/services/triggers.py +++ b/services/triggers.py @@ -1,7 +1,8 @@ import asyncio import json -from sqlalchemy import event, select +from sqlalchemy import event, select, union +from sqlalchemy.orm import aliased from orm.author import Author, AuthorFollower from orm.reaction import Reaction @@ -81,17 +82,13 @@ def after_reaction_update(mapper, connection, reaction: Reaction): .where(Reaction.id == reaction.reply_to) ) - author_query = ( - select(author_subquery.subquery()) - .select_from(author_subquery.subquery()) - .union( - select(replied_author_subquery.subquery()).select_from( - replied_author_subquery.subquery() - ) - ) - ) + author_aliased_query = aliased(union(author_subquery, replied_author_subquery)) - for author_with_stat in get_with_stat(author_query): + # Get authors with stat + authors_with_stat = get_with_stat(author_aliased_query) + + # Cache authors + for author_with_stat in authors_with_stat: asyncio.create_task(cache_author(author_with_stat.dict())) shout_query = select(Shout).select_from(Shout).where(Shout.id == reaction.shout)