map Comment like this:

from sqlalchemy.orm import query_expression

class Comment(Base):
    __tablename__ = 'comment'
    id = Column(Integer, primary_key=True)
    news_id = Column(Integer, ForeignKey(News.id))

    is_read = query_expression()


then you need to use options in your query to link the join to your
subquery, as well as the is_read expression, to that object:

from sqlalchemy.orm import contains_eager

comment_read_query = (
    session.query(CommentRead).filter(
        CommentRead.comment_id == Comment.id,
        CommentRead.user_id == 1,  # Interested in reads by specific user
    ).exists().label('is_read')
)
comment_query = session.query(Comment, comment_read_query).subquery()
news = session.query(News).outerjoin(comment_query).options(
    contains_eager(News.comments, alias=comment_query).with_expression(
        Comment.is_read, comment_query.c.is_read)
).all()




On Fri, May 25, 2018 at 11:28 AM, Marat Sharafutdinov <deca...@gmail.com> wrote:
> The news has comments, the comments have readings by users.
> I need to receive all the news with comments and readings of these comments
> by a specific user.
> The query is ok, but I can't get the value of the column `is_read` as an
> attribute of the comment object.
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to