Try closing the session.  I've used similar code in my projects.  Also if 
you're using SQLite you need to do some additional tweaking so that it 
understands the transaction.

def setUp(self):
    self.session = sessionmaker(bind=engine)()
    self.session.begin_nested()

def tearDown(self):
    self.session.rollback()
    self.session.close()

#
# SQLite specific events
#

@classmethod
def setUpClass(cls):
    """Create the database."""
    global engine

    engine = create_engine('sqlite:///sqlalchemy.db')

    @event.listens_for(engine, "connect")
    def do_connect(dbapi_connection, connection_record):
        dbapi_connection.isolation_level = None

    @event.listens_for(engine, "begin")
    def do_begin(conn):
        conn.execute("BEGIN")

    Base.metadata.create_all(engine)


@classmethod
def tearDownClass(cls):
    """Destroy the database."""
    engine.dispose()

On Thursday, October 27, 2016 at 5:13:48 PM UTC-4, Zach wrote:
>
> Hi, I'd really appreciate some assistance with the below issue.
>
>
> Rolling back sqlalchemy transactions cross-flush currently appears 
> impossible to me. It’s necessary if you want to issue queries that rely on 
> the presence of an assigned primary key identifier (adding something to 
> the session isn’t sufficient to get a primary key assigned).
>
>
> But if you issue such a query, you’ll either get a query-invoked autoflush 
> (if autoflush is on), or you’ll have to flush first. And this apparently 
> seems to changes the scope of what gets rolled back on session.rollback(). 
> It seems to be only the stuff after the flush.
>
>
> Use case: rolling back on the teardown method of unit tests that require 
> flushes.
>
>
> Solution I'm looking for: A way to roll back *all* uncommitted changes 
> after a savepoint/virtual transaction/some other kind of transactional 
> wrapper is created in the setUp method of a unittest.TestCase subclass.
>
>
> Example:
>
>
> session = sessionmaker()
> class MyTest(unittest.TestCase):
>
>     def setUp(self):
>         session.begin_nested()
>
>     def tearDown(self):
>         session.rollback()
>
>     def myTest(self):
>         session.add(object) # now a flush is required because `id` is used 
> below
>         query = session.query('select id from my_table where id = 
> {}'.format(object.id))
>         # Problem: Now `object` will exist even after `tearDown`
>
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
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.

Reply via email to