This is a spin-off from the thread on how to do setup/test-app/teardown: http://groups.google.com/group/sqlalchemy/browse_thread/thread/4fd6344bf8b9c033 (thanks for everyone's help on that)
I had posted test code that illustrates it's possible to save/delete a mapped object using two different sessions indepedently. But now, when I try using a mapped class that has been assigned a session, that is, Session.mapper() instead of mapper(), the test errors in InvalidRequestError: Object '[EMAIL PROTECTED]' is already attached to session '18333488' (this is '18402096') but, since I am explicitly saving it to a new session and the session has a different scope, shouldn't this be possible? Maybe I'm not fully understanding "scoped" sessions. FWIW, if I del so._sa_session_id before fixture_session.save(so) then the assertions later on all pass. Not that I'm going to do this, promise ;) -Kumar PS. I thought for a minute I needed save_on_init=False but this didn't solve it. from sqlalchemy import * from sqlalchemy.orm import scoped_session, sessionmaker, mapper from sqlalchemy.exceptions import IntegrityError PrivateSession = scoped_session( sessionmaker(autoflush=False, transactional=True), scopefunc=lambda:__name__) # a private scope AppSession = scoped_session( sessionmaker(autoflush=False, transactional=True)) dsn = 'sqlite:///:memory:' def test_sa_scoping(): engine = create_engine(dsn) metadata = MetaData() sometable = Table('sometable', metadata, Column('id', Integer, primary_key=True), Column('keyname', String(30), unique=True)) class SomeObject(object): pass conn = engine.connect() AppSession.configure(bind=conn) app_session = AppSession() metadata.create_all(bind=engine) # when this is just mapper(), the test passes AppSession.mapper(SomeObject, sometable) conn = engine.connect() PrivateSession.configure(bind=conn) trans = conn.begin() fixture_session = PrivateSession() # create some data to test with : so = SomeObject() so.keyname = "some unique key name" fixture_session.save(so) fixture_session.flush() trans.commit() trans = conn.begin() so2 = SomeObject() so2.keyname = "some unique key name" app_session.save(so2) try: app_session.flush() except IntegrityError: # violated unique key trans.rollback() else: trans.commit() app_session.close() # after testing application code, I want to tear down # test even if the app had an error : assert so in fixture_session trans = conn.begin() fixture_session.delete(so) fixture_session.flush() trans.commit() rs = fixture_session.query(SomeObject).all() assert rs == [], "unexpected: %s" % rs if __name__ == '__main__': test_sa_scoping() Traceback (most recent call last): File "test_sa_scoping.py", line 65, in ? test_sa_scoping() File "test_sa_scoping.py", line 37, in test_sa_scoping fixture_session.save(so) File "/Users/kumar/env/sqlalchemy-exp/lib/python2.4/site-packages/SQLAlchemy-0.4.2p3-py2.4.egg/sqlalchemy/orm/session.py", line 822, in save self._save_impl(instance, entity_name=entity_name) File "/Users/kumar/env/sqlalchemy-exp/lib/python2.4/site-packages/SQLAlchemy-0.4.2p3-py2.4.egg/sqlalchemy/orm/session.py", line 993, in _save_impl self._attach(instance) File "/Users/kumar/env/sqlalchemy-exp/lib/python2.4/site-packages/SQLAlchemy-0.4.2p3-py2.4.egg/sqlalchemy/orm/session.py", line 1029, in _attach raise exceptions.InvalidRequestError("Object '%s' is already attached " sqlalchemy.exceptions.InvalidRequestError: Object '[EMAIL PROTECTED]' is already attached to session '18333488' (this is '18402096') --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---