On Nov 9, 2011, at 3:37 AM, sector119 wrote: > > ### TEST 2 > # HERE I GOT ERROR if I uncomment "user = ..." line > > session = Session() > > report = session.query(Report).get(1) > #user = session.query(User).filter_by(username='sector119').one() > comment = Comment(content=u'test comment', user=user) > report.comments.append(comment) > session.add(report) > session.flush() > session.commit() > > session.close() >
issue is here: # create new Comment object, user=user means "comment" is now present # in the Session. The "report" reference is None. comment = Comment(content=u'test comment', user=user) # steps here are: # 1. access report.comments # 2. report.comments not present, loads from the database # 3. autoflush # 4. append "comment" to collection report.comments.append(comment) the above fails as autoflush flushes the incomplete "comment" object. this can be seen in the stack trace: > self.session._autoflush() > File "/home/eps/devel/tourclub/sqlalchemy/lib/sqlalchemy/orm/session.py", > line 973, in _autoflush solution is to set the "report" on the "comment" instead, or to temporarily disable autoflush, such as at http://www.sqlalchemy.org/trac/wiki/UsageRecipes/DisableAutoflush -- 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.
