stat-aliased-select-fix
All checks were successful
Deploy on push / deploy (push) Successful in 1m10s

This commit is contained in:
Untone 2024-06-12 12:26:53 +03:00
parent c24f3bbb4a
commit 9580282c79

View File

@ -46,26 +46,29 @@ def add_topic_stat_columns(q):
def add_author_stat_columns(q): def add_author_stat_columns(q):
# Соединяем таблицу Author с таблицей ShoutAuthor и таблицей Shout с использованием INNER JOIN # Алиасирование таблиц для предотвращения конфликтов имен
aliased_shout_author = aliased(ShoutAuthor)
aliased_shout = aliased(Shout)
aliased_author_follower = aliased(AuthorFollower)
q = ( q = (
q.select_from(Author) q.select_from(Author)
.join( .join(
ShoutAuthor, aliased_shout_author,
ShoutAuthor.author == Author.id, aliased_shout_author.author == Author.id,
) )
.join( .join(
Shout, aliased_shout,
and_( and_(
Shout.id == ShoutAuthor.shout, aliased_shout.id == aliased_shout_author.shout,
Shout.deleted_at.is_(None), aliased_shout.deleted_at.is_(None),
), ),
) )
.add_columns(func.count(distinct(Shout.id)).label("shouts_stat")) .add_columns(func.count(distinct(aliased_shout.id)).label("shouts_stat"))
) )
# Соединяем таблицу Author с таблицей AuthorFollower с использованием LEFT OUTER JOIN q = q.outerjoin(aliased_author_follower, aliased_author_follower.author == Author.id).add_columns(
q = q.outerjoin(AuthorFollower, AuthorFollower.author == Author.id).add_columns( func.count(distinct(aliased_author_follower.follower)).label("followers_stat")
func.count(distinct(AuthorFollower.follower)).label("followers_stat")
) )
q = q.group_by(Author.id) q = q.group_by(Author.id)
@ -73,6 +76,7 @@ def add_author_stat_columns(q):
return q return q
def get_topic_shouts_stat(topic_id: int): def get_topic_shouts_stat(topic_id: int):
q = ( q = (
select(func.count(distinct(ShoutTopic.shout))) select(func.count(distinct(ShoutTopic.shout)))