Hi,
I’ve run into a rather interesting issue, where calling session.rollback() dies. The error I get with SQLA 0.7 is:
<class 'sqlalchemy.exc.InvalidRequestError'>: Can't attach instance <Foo at 0x2acf68808710>; another instance with key (<class 'module.Foo'>, (342,)) is already present in this session.
Traceback:
sqlalchemy-rel_0_7/lib/sqlalchemy/orm/session.py:672:rollback
sqlalchemy-rel_0_7/lib/sqlalchemy/orm/session.py:386:rollback
sqlalchemy-rel_0_7/lib/sqlalchemy/orm/session.py:417:_rollback_impl
sqlalchemy-rel_0_7/lib/sqlalchemy/orm/session.py:274:_restore_snapshot
sqlalchemy-rel_0_7/lib/sqlalchemy/orm/session.py:1620:_update_impl
sqlalchemy-rel_0_7/lib/sqlalchemy/orm/session.py:1648:_attach
0.8 gives a slightly different message, but I think it is essentially the same:
<type 'exceptions.AssertionError'>: A conflicting state is already present in the identity map for key (<class 'module.Foo'>, (342,))
Traceback:
sqlalchemy-default/lib/sqlalchemy/orm/session.py:637:rollback
sqlalchemy-default/lib/sqlalchemy/orm/session.py:346:rollback
sqlalchemy-default/lib/sqlalchemy/orm/session.py:377:_rollback_impl
sqlalchemy-default/lib/sqlalchemy/orm/session.py:233:_restore_snapshot
sqlalchemy-default/lib/sqlalchemy/orm/session.py:1578:_update_impl
sqlalchemy-default/lib/sqlalchemy/orm/identity.py:115:add
What I see when turning on query logging:
- The Foo object with primary key 342 is loaded (using a non-primary mapper, if that makes any difference), and then deleted. The session is flushed.
- A new object is created (this time using the primary mapper), and during INSERT, SQLite assigns the same primary key to it as the previously deleted object had (I can see that from subsequent SELECT statements referencing the new object; it would be nice if SQLA logged the primary key after an INSERT at debug level)
- An application error (unrelated to SQLAlchemy) occurs, which causes rollback() to be called
I have a reliable reproducer, but it is the unit test of a large application, so there are lots of possibly unrelated things going on. I’ve spent two days coming up with a standalone reproducer, but I’ve failed, that’s why I’m sending this mail instead of opening a ticket.
Any ideas?
Gabor
Hi,I’ve run into a rather interesting issue, where calling session.rollback() dies. The error I get with SQLA 0.7 is:
What I see when turning on query logging:- The Foo object with primary key 342 is loaded (using a non-primary mapper, if that makes any difference), and then deleted. The session is flushed.- A new object is created (this time using the primary mapper), and during INSERT, SQLite assigns the same primary key to it as the previously deleted object had (I can see that from subsequent SELECT statements referencing the new object; it would be nice if SQLA logged the primary key after an INSERT at debug level)- An application error (unrelated to SQLAlchemy) occurs, which causes rollback() to be calledI have a reliable reproducer, but it is the unit test of a large application, so there are lots of possibly unrelated things going on. I’ve spent two days coming up with a standalone reproducer, but I’ve failed, that’s why I’m sending this mail instead of opening a ticket.
s = Session(e)a1 = A(id=1, data='a1')a2 = A(id=2, data='a2')s.add(a1)s.add(a2)s.commit()
s.delete(a1)s.flush()
a2.id = 1s.flush()
s.execute("insert into a (id, data) values (1, 'a3')")a3 = s.query(A).get(1)
Catching both of these is easy enough and a patch is added to #2583: http://www.sqlalchemy.org/trac/ticket/2583The SQLite case you describe isn't exactly like either of these; if the object were flushed by the session and received the same PK, that case is already handled since the object would be in ._new. So my only guess is that, that's not the trigger of the issue. So at the very least you need to try that patch and see if it clears things up.Assuming that's the fix, I don't know that there's a clean way to patch this in, except for patching your install or monkeypatching a new Session class in. I should have it committed in both 0.7/0.8 within 30, unless I hit some snags in the tests.
I’ve tested the 0.7 & 0.8 snapshots, and both work fine now. Thanks!
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to
sqlal...@googlegroups.com.
To unsubscribe from this group, send email to
sqlalchemy+...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.