[Python] SQLAlchemy - using hybrid property in queries

Discussão em 'Python' iniciado por Stack, Janeiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    Say I have a Question which belongs to some Homework. A Question can be correctly answered, and Homework is passed if all Questions are correct.

    class Question(Base):
    __tablename__ = 'questions'
    id: Mapped[int] = mapped_column(primary_key=True)
    answered_correctly: Mapped["bool"] = mapped_column()
    homework: Mapped["Homework"] = relationship(back_populates="questions")
    homework_id: Mapped[int] = mapped_column(ForeignKey("homework.id"))


    class Homework(Base):
    __tablename__ = 'homework'
    id: Mapped[int] = mapped_column(primary_key=True)
    answered_correctly: Mapped["bool"] = mapped_column()
    questions: Mapped[list["Questions"]] = relationship(
    back_populates="homework", default_factory=list
    )

    @hybrid_property
    def passed(self):
    return all([question.answered correctly for question in self.questions])


    How do I get this to work in an SQL query? I know that I have to implement passed.expression, but I'm unclear on what exactly it should return.

    I have an SQL query that I can use to find homework that isn't passed - do I have to translate this query to SQLAlchemy's API, & that's what passed.expression is supposed to return?

    SELECT homework.id, from homework
    JOIN question on question.id = homework.id
    WHERE homework.id = &1
    GROUP BY homework.id
    HAVING COUNT(CASE WHEN question.answered_correctly = FALSE THEN 1 END) > 0

    Continue reading...

Compartilhe esta Página