This commit is contained in:
Untone 2024-08-07 15:04:17 +03:00
parent c601fcc2a4
commit ec7b25df3c

View File

@ -39,17 +39,26 @@ def query_shouts():
select( select(
Shout.id.label("shout_id"), Shout.id.label("shout_id"),
func.json_agg( func.json_agg(
distinct(
func.json_build_object( func.json_build_object(
'id', Author.id, 'id', Author.id,
'name', Author.name, 'name', Author.name,
'slug', Author.slug, 'slug', Author.slug,
'pic', Author.pic 'pic', Author.pic
) )
)
).label("authors") ).label("authors")
) )
.select_from(
select(
distinct(Author.id),
Author.name,
Author.slug,
Author.pic,
Shout.id.label("shout_id")
)
.join(ShoutAuthor, ShoutAuthor.author == Author.id) .join(ShoutAuthor, ShoutAuthor.author == Author.id)
.join(Shout, Shout.id == ShoutAuthor.shout)
.subquery()
)
.group_by(Shout.id) .group_by(Shout.id)
.subquery() .subquery()
) )
@ -58,17 +67,26 @@ def query_shouts():
select( select(
Shout.id.label("shout_id"), Shout.id.label("shout_id"),
func.json_agg( func.json_agg(
distinct(
func.json_build_object( func.json_build_object(
'id', Topic.id, 'id', Topic.id,
'title', Topic.title, 'title', Topic.title,
'body', Topic.body, 'body', Topic.body,
'slug', Topic.slug 'slug', Topic.slug
) )
)
).label("topics") ).label("topics")
) )
.select_from(
select(
distinct(Topic.id),
Topic.title,
Topic.body,
Topic.slug,
Shout.id.label("shout_id")
)
.join(ShoutTopic, ShoutTopic.topic == Topic.id) .join(ShoutTopic, ShoutTopic.topic == Topic.id)
.join(Shout, Shout.id == ShoutTopic.shout)
.subquery()
)
.group_by(Shout.id) .group_by(Shout.id)
.subquery() .subquery()
) )