Could it be that a difference between the declarative for a table and
the underlying table could result in the failure as first described?
In the unit test, setUp deletes all tables in the setup, then
recreates all tables anew, then populates them. Since then there have
not been any issues.

(I guess I should look into Alembic, eh?)

Thanks for your help.

On Feb 6, 11:36 am, cbc <clayton.cafi...@mondaylambson.com> wrote:
> 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 <mike...@zzzcomputing.com> 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 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