switching primary keys breaks the ORM

Issue #1519 resolved
faassen created an issue

When switching primary keys from one ORM-ed object to another a problem occurs: an attempt is made to remove an object from a session that does not exist anymore. The attached 'reproduce.py' shows this problem, though it only shows up with particular dictionary orderings and can therefore be platform dependent. If you do not get the error, reverse the following in the test script to trigger it:

a_editable.status = PUBLISHED
a_published.status = ARCHIVED

Tests need to be added (to test/orm/test_naturalpks) that demonstrate this behavior (both orderings need to be tested to trigger the problem on all platforms).

A possible fix would be the following change:

Index: lib/sqlalchemy/orm/session.py
===================================================================
--- lib/sqlalchemy/orm/session.py   (revision 6289)
+++ lib/sqlalchemy/orm/session.py   (working copy)
@@ -1018,7 +1018,7 @@
                 state.key = instance_key
             elif state.key != instance_key:
                 # primary key switch
-                self.identity_map.remove(state)
+                self.identity_map.discard(state)
                 state.key = instance_key

             self.identity_map.replace(state)

Comments (3)

  1. Log in to comment