]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
if an object fails construction, doesnt get added to the session
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 17 Jun 2006 00:45:45 +0000 (00:45 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 17 Jun 2006 00:45:45 +0000 (00:45 +0000)
CHANGES
lib/sqlalchemy/orm/mapper.py
test/orm/mapper.py

diff --git a/CHANGES b/CHANGES
index 9161965fd315cb85e630d1b7b3d903c37193a4c0..9b80778efacfb9971bafd01e85683f431bafc92c 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -38,7 +38,8 @@ __doc__ from the original class
 function at the moment
 - some fixes to between(), column.between() to propigate typing
 information better [ticket:202]
-
+- if an object fails to be constructed, is not added to the 
+session [ticket:203]
 
 0.2.2
 - big improvements to polymorphic inheritance behavior, enabling it
index 90db7f83e5103f3fcf352e56c89b7d0ca786299c..ac8d6f330fa32e9632ae531bb0d0886468d07847 100644 (file)
@@ -468,7 +468,12 @@ class Mapper(object):
                 session._register_new(self)
 
             if oldinit is not None:
-                oldinit(self, *args, **kwargs)
+                try:
+                    oldinit(self, *args, **kwargs)
+                except:
+                    if session is not None:
+                        session.expunge(self)
+                    raise
         # override oldinit, insuring that its not already a Mapper-decorated init method
         if oldinit is None or not hasattr(oldinit, '_sa_mapper_init'):
             init._sa_mapper_init = True
index 96b092d89d9ccb0a3be055cb214f833e1ab1b2af..b73f20b1501c9e8d6576c3429550a7acb2b5e242 100644 (file)
@@ -135,6 +135,24 @@ class MapperTest(MapperSuperTest):
         self.assert_(u.user_name == 'jack')
         self.assert_(a not in u.addresses)
 
+    def testbadconstructor(self):
+        """tests that if the construction of a mapped class fails, the instnace does not get placed in the session"""
+        class Foo(object):
+            def __init__(self, one, two):
+                pass
+        mapper(Foo, users)
+        sess = create_session()
+        try:
+            Foo('one', _sa_session=sess)
+            assert False
+        except:
+            assert len(list(sess)) == 0
+        try:
+            Foo('one')
+            assert False
+        except TypeError, e:
+            pass
+            
     def testrefresh_lazy(self):
         """tests that when a lazy loader is set as a trigger on an object's attribute (at the attribute level, not the class level), a refresh() operation doesnt fire the lazy loader or create any problems"""
         s = create_session()