On Jul 10, 4:29 pm, Michael Bayer <mike...@zzzcomputing.com> wrote:
> On Jul 10, 8:37 am, Ste <stefan.aeschbac...@gmail.com> wrote:
>
>
>
>
>
> > Hello Group,
>
> > I see the following behaviour which I can not explain (don't know
> > sqlalchemy too well though):
>
> > I have the following table setup (heavily simplified):
>
> > class WikiPage(Base):
> >     __tablename__ = 'wiki_page'
> >     id = Column(Integer, primary_key=True)
> >     name = Column(Unicode(40))
> >     content = Column(UnicodeText())
>
> >     def method_that_changes_something(self,...):
> >           ....
>
> > class Problem(WikiPage):
> >     __tablename__ = 'problem'
> >     __mapper_args__ = {'polymorphic_identity': 'problem'}
>
> >     id = Column(Integer, ForeignKey('wiki_page.id'), primary_key=True)
>
> > Sometimes (so far only when run from a nose test suite) when I query
> > like this:
>
> > problem = self.Session.query(Problem).filter_by(id=problem_id).first()
>
> > where problem_id is None. I receive the unexpected result:
> >  - not problem is False
> >  - when applying str to the result I get: <my_app.model.Problem object
> > at 0x1040db710>
> >  - there should be no Problems with id Null (though currently not
> > InnoDB tables, all pending changes have been commited and I do not see
> > any rows with NULL Ids in the DB)
> >  - when I try to access problem.id I get: AttributeError: 'NoneType'
> > object has no attribute 'id'
> >  - the same when I try to access problem.name
> >  - problem.method_that_changes_something can be called and does
> > something (on an object that has been inserted before this query but
> > this is already commited)
> >  - adding sleep(1) before the query changes nothing (so it does not
> > seem to be a race condition)
>
> > Environement:
> > - Sqlalchemy 0.5.8 or 0.6.2 (behaves the same)
> > - Pylons 1.0
> > - MySQL Python 1.2.3
>
> > So my questsions are:
> > - Am I doing something wrong?
> > - How can I persuade nosetests to output sqlalchemy log messages?
>
> you'd want to put "echo='debug'" on your create_engine() statement, or
> if using nose add "--log-debug=sqlalchemy.engine" to the command line.
>
> You should be doing a pdb here so that you can more closely inspect
> the object you're getting back and further experimenting with the
> query object.   I would suspect the means of testing the result is
> where the meaning of "problem" is being changed - pdb will eliminate
> that.

Thanks, that helped, now I know a little more (but still not
conclusive):

I use the following:

problem = self._problem_q.filter_by(id=problem_id).first()
if problem: raise

problem is a normal database object. The strange behaviour I described
before came from a
debugging error I made (I made checks at different times in the
program flow).
So the nature of the object is no longer an issue.

What I still don't understand is, that if I run the same query in pdb
immediately after the raise, I get back
None (which is what I would expected).

The log showing the query from the program and the query from pdb:
INFO:sqlalchemy.engine.base.Engine.0x...6490:BEGIN
INFO:sqlalchemy.engine.base.Engine.0x...6490:SELECT wiki_page.type AS
wiki_page_type, wiki_page.id AS wiki_page_id, problem.id AS
problem_id, wiki_page.name AS wiki_page_name, wiki_page.content AS
wiki_page_content, wiki_page.timestamp AS wiki_page_timestamp,
wiki_page.creator AS wiki_page_creator, wiki_page.creator_ip AS
wiki_page_creator_ip, problem.discussion_id AS problem_discussion_id
FROM wiki_page INNER JOIN problem ON wiki_page.id = problem.id
WHERE wiki_page.id IS NULL
 LIMIT 0, 1
INFO:sqlalchemy.engine.base.Engine.0x...6490:[]
DEBUG:sqlalchemy.engine.base.Engine.0x...6490:Col ('wiki_page_type',
'wiki_page_id', 'problem_id', 'wiki_page_name', 'wiki_page_content',
'wiki_page_timestamp', 'wiki_page_creator', 'wiki_page_creator_ip',
'problem_discussion_id')
DEBUG:sqlalchemy.engine.base.Engine.0x...6490:Row ('problem', 2L, 2L,
'Double name test', 'This is a problem for the double name test.',
datetime.datetime(2010, 7, 11, 11, 30, 57), None, '127.0.0.1', 1L)
> /Users/saes/work/my_app/src/my_app/my_app/controllers/problem.py(99)save()
-> if problem: raise
(Pdb) self._problem_q.filter_by(id=problem_id).first()
INFO:sqlalchemy.engine.base.Engine.0x...6490:BEGIN
INFO:sqlalchemy.engine.base.Engine.0x...6490:SELECT wiki_page.type AS
wiki_page_type, wiki_page.id AS wiki_page_id, problem.id AS
problem_id, wiki_page.name AS wiki_page_name, wiki_page.content AS
wiki_page_content, wiki_page.timestamp AS wiki_page_timestamp,
wiki_page.creator AS wiki_page_creator, wiki_page.creator_ip AS
wiki_page_creator_ip, problem.discussion_id AS problem_discussion_id
FROM wiki_page INNER JOIN problem ON wiki_page.id = problem.id
WHERE wiki_page.id IS NULL
 LIMIT 0, 1
INFO:sqlalchemy.engine.base.Engine.0x...6490:[]
DEBUG:sqlalchemy.engine.base.Engine.0x...6490:Col ('wiki_page_type',
'wiki_page_id', 'problem_id', 'wiki_page_name', 'wiki_page_content',
'wiki_page_timestamp', 'wiki_page_creator', 'wiki_page_creator_ip',
'problem_discussion_id')
(Pdb) type(problem_id)
<type 'NoneType'>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@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