This commit is contained in:
parent
c551ca2e70
commit
f9a91e3a66
|
@ -34,10 +34,10 @@ def query_shouts():
|
||||||
# Создаем алиасы для таблиц для избежания конфликтов имен
|
# Создаем алиасы для таблиц для избежания конфликтов имен
|
||||||
aliased_reaction = aliased(Reaction)
|
aliased_reaction = aliased(Reaction)
|
||||||
|
|
||||||
# Используем SQLAlchemy для подзапросов с уникальными значениями
|
# Подзапрос для уникальных авторов
|
||||||
authors_subquery = (
|
authors_subquery = (
|
||||||
select(
|
select(
|
||||||
Shout.id.label("shout_id"),
|
ShoutAuthor.shout.label("shout_id"),
|
||||||
func.json_agg(
|
func.json_agg(
|
||||||
func.json_build_object(
|
func.json_build_object(
|
||||||
'id', Author.id,
|
'id', Author.id,
|
||||||
|
@ -47,25 +47,15 @@ def query_shouts():
|
||||||
)
|
)
|
||||||
).label("authors")
|
).label("authors")
|
||||||
)
|
)
|
||||||
.select_from(
|
.join(Author, ShoutAuthor.author == Author.id)
|
||||||
select(
|
.group_by(ShoutAuthor.shout)
|
||||||
distinct(Author.id),
|
|
||||||
Author.name,
|
|
||||||
Author.slug,
|
|
||||||
Author.pic,
|
|
||||||
Shout.id.label("shout_id")
|
|
||||||
)
|
|
||||||
.join(ShoutAuthor, ShoutAuthor.author == Author.id)
|
|
||||||
.join(Shout, Shout.id == ShoutAuthor.shout)
|
|
||||||
.subquery()
|
|
||||||
)
|
|
||||||
.group_by(Shout.id)
|
|
||||||
.subquery()
|
.subquery()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Подзапрос для уникальных тем
|
||||||
topics_subquery = (
|
topics_subquery = (
|
||||||
select(
|
select(
|
||||||
Shout.id.label("shout_id"),
|
ShoutTopic.shout.label("shout_id"),
|
||||||
func.json_agg(
|
func.json_agg(
|
||||||
func.json_build_object(
|
func.json_build_object(
|
||||||
'id', Topic.id,
|
'id', Topic.id,
|
||||||
|
@ -75,19 +65,8 @@ def query_shouts():
|
||||||
)
|
)
|
||||||
).label("topics")
|
).label("topics")
|
||||||
)
|
)
|
||||||
.select_from(
|
.join(Topic, ShoutTopic.topic == Topic.id)
|
||||||
select(
|
.group_by(ShoutTopic.shout)
|
||||||
distinct(Topic.id),
|
|
||||||
Topic.title,
|
|
||||||
Topic.body,
|
|
||||||
Topic.slug,
|
|
||||||
Shout.id.label("shout_id")
|
|
||||||
)
|
|
||||||
.join(ShoutTopic, ShoutTopic.topic == Topic.id)
|
|
||||||
.join(Shout, Shout.id == ShoutTopic.shout)
|
|
||||||
.subquery()
|
|
||||||
)
|
|
||||||
.group_by(Shout.id, Shout.authors, Shout.topics)
|
|
||||||
.subquery()
|
.subquery()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -104,14 +83,14 @@ def query_shouts():
|
||||||
)
|
)
|
||||||
).label("rating_stat"),
|
).label("rating_stat"),
|
||||||
func.max(aliased_reaction.created_at).label("last_reacted_at"),
|
func.max(aliased_reaction.created_at).label("last_reacted_at"),
|
||||||
func.coalesce(authors_subquery.c.authors, "[]").label("authors"),
|
authors_subquery.c.authors.label("authors"),
|
||||||
func.coalesce(topics_subquery.c.topics, "[]").label("topics"),
|
topics_subquery.c.topics.label("topics"),
|
||||||
)
|
)
|
||||||
.outerjoin(aliased_reaction, aliased_reaction.shout == Shout.id)
|
.outerjoin(aliased_reaction, aliased_reaction.shout == Shout.id)
|
||||||
.outerjoin(authors_subquery, authors_subquery.c.shout_id == Shout.id)
|
.outerjoin(authors_subquery, authors_subquery.c.shout_id == Shout.id)
|
||||||
.outerjoin(topics_subquery, topics_subquery.c.shout_id == Shout.id)
|
.outerjoin(topics_subquery, topics_subquery.c.shout_id == Shout.id)
|
||||||
.where(and_(Shout.published_at.is_not(None), Shout.deleted_at.is_(None)))
|
.where(and_(Shout.published_at.is_not(None), Shout.deleted_at.is_(None)))
|
||||||
.group_by(Shout.id)
|
.group_by(Shout.id, authors_subquery.c.authors, topics_subquery.c.topics)
|
||||||
)
|
)
|
||||||
|
|
||||||
return q, aliased_reaction
|
return q, aliased_reaction
|
||||||
|
|
Loading…
Reference in New Issue
Block a user