Michael:

I created a full reproducing test (http://pastebin.com/vutfUgpk) and
the test ... WORKS!

So, there's obviously something else creeping in here somewhere. It
was useful for me to create the test. Thanks for the suggestion.

I will close the loop when I find the cause of my original issue.

THANKS

-C

On Feb 3, 9:45 pm, Michael Bayer <[email protected]> wrote:
> On Feb 3, 2012, at 11:28 AM, cbc 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?
>
> The outerjoin here is from Question to Answer so you should never have None 
> in the first element of the tuple.  I'd need a full reproducing test to see 
> exactly what would cause Question to be retuned as None.

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

Reply via email to