есть код:
query = select(Review).options(selectinload(Review.prize)
.options(selectinload(ReceivedPrize.prize)),
selectinload(Review.author)).where(Review.id == review_id)
likes_query = select(
func.count(case((ReviewLike.like == True, 1))).label("likes_count"),
func.count(case((ReviewLike.like == False, 1))).label("dislikes_count")
).where(ReviewLike.review_id == review_id)
user_like_query = select(ReviewLike).where(
ReviewLike.review_id == review_id,
ReviewLike.user_id == user_id
)
models:
class Review(Base):
__tablename__ = "review"
id = Column(Integer, primary_key=True, autoincrement=True)
author_id = Column(Integer, ForeignKey('user.id'), nullable=False)
prize_id = Column(Integer, ForeignKey('received_prize.id'), nullable=False)
title = Column(String, nullable=False)
description = Column(String, nullable=False)
media = Column(ARRAY(String), nullable=True)
created_at = Column(TIMESTAMP, server_default=func.current_timestamp())
author = relationship("User", back_populates="reviews", lazy="select")
prize = relationship("ReceivedPrize", back_populates="reviews", lazy="select")
likes = relationship("ReviewLike", back_populates="review", lazy="select")
class ReviewLike(Base):
__tablename__ = "review_like"
id = Column(Integer, primary_key=True, autoincrement=True)
user_id = Column(Integer, ForeignKey('user.id'), nullable=False)
review_id = Column(Integer, ForeignKey('review.id'), nullable=False)
like = Column(Boolean, nullable=False)
created_at = Column(TIMESTAMP, server_default=func.current_timestamp())
user = relationship("User", back_populates="likes", lazy="select")
review = relationship("Review", back_populates="likes", lazy="select")
Сейчас все эти запросы нормально работают. Но мне нужно, user_like_query и likes_query засунуть подзапросом в query. Никак не получается это сделать.