In the unit tests for one module of my app, I'm using the following setup: class SomeTestCase(TestCase): def __init__(self, *args, **kwargs): TestCase.__init__(self, *args, **kwargs) engine = create_engine("postgresql+psycopg2://...", echo=False) self.session = scoped_session(sessionmaker(bind=engine))() ... def setUp(self): self.session.begin_nested()
def tearDown(self): self.session.rollback() def test_somefunction(self): self.session.begin_nested() try: # add Object1 session.flush() # To retrieve Object1.id, which does work and is returned on attribute access ... # add some other objects, including Object2 of class Thing ... session.flush() session.commit() except: session.rollback() raise else: object_id = Object2.id # Also works session.query(Thing).filter(Thing.id = object_id).one() # Raises a NoResultFound exception: "No row was found for one()" After some setup and other operations, the generated SQL shows savepoints 1 and 2 are created, followed by various insertions and updates, then savepoint 2 is released, and finally it ends with a rollback to savepoint 1. With two commits instead of one the problem persists, while three commits fixes the problem but then the changes aren't removed after calling rollback (which I suppose makes sense since it would have then committed at the top session scope). Any ideas on how to solve this would be greatly appreciated. -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.