I should add: First element is always none _even if question has been
answered by that user_

On Feb 3, 11:28 am, cbc <clayton.cafi...@mondaylambson.com> wrote:
> Hi:
>
> I'm having trouble getting a subquery to return entities.
>
> class Question(Base):
>         __tablename__ = 'question'
>         question_id = Column(INTEGER(unsigned=True), primary_key=True)
>         ...etc
>         q_answers = relationship("Answer", backref="question")
>
> class Answer(Base):
>         __tablename__ = 'answer'
>         answer_id = Column(INTEGER(unsigned=True), primary_key=True)
>         user_id = Column(INTEGER(unsigned=True), ForeignKey('user.user_id'),
> nullable=False)
>         question_id = Column(INTEGER(unsigned=True),
> ForeignKey('question.question_id'), nullable=False)
>         ...etc
>
> stmt = session.query(Answer).filter(Answer.user_id ==
> user_id).subquery()
> answers = aliased(Answer, stmt)
> query = session.query(Question, answers)\
>         .outerjoin(answers, Question.q_answers)\
>         .filter(Question.question_group_id == question_group_id)
> questions = query.all()
>
> This generates MySQL that returns all desired columns and returns NULL
> if question has not yet been answered by the specified user. Groovy so
> far.
>
> I was expecting tuples in the list of questions (Answer, Question),
> but the first element is always None. e.g.
>
> >>> dir(questions[0])
>
> [None, 'Question', ... etc
>
> So while I'm expecting the subquery results to be understood as
> entities (Answer), this isn't happening. But I cannot figure out why.
>
> Where have I failed?
>
> Thanks!
>
> -C

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to