]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
added "enable_typechecks=True" flag on relation so the new type check from #500 can...
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 6 Mar 2007 16:47:43 +0000 (16:47 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 6 Mar 2007 16:47:43 +0000 (16:47 +0000)
lib/sqlalchemy/orm/dependency.py
lib/sqlalchemy/orm/properties.py
test/orm/relationships.py

index 0b3b70d219f86bd44ed63fc65cb6cc32a18dc128..a528c13274028af6383d0aae4b3f45d04f4a5fc7 100644 (file)
@@ -39,6 +39,7 @@ class DependencyProcessor(object):
         self.post_update = prop.post_update
         self.foreign_keys = prop.foreign_keys
         self.passive_deletes = prop.passive_deletes
+        self.enable_typechecks = prop.enable_typechecks
         self.key = prop.key
 
         self._compile_synchronizers()
@@ -101,8 +102,10 @@ class DependencyProcessor(object):
         raise NotImplementedError()
 
     def _verify_canload(self, child):
+        if not self.enable_typechecks:
+            return
         if child is not None and not self.mapper.canload(child):
-            raise exceptions.FlushError("Attempting to flush an item of type %s on collection '%s', which is handled by mapper '%s' and does not load items of that type.  Did you mean to use a polymorphic mapper for this relationship ?" % (child.__class__, self.prop, self.mapper))
+            raise exceptions.FlushError("Attempting to flush an item of type %s on collection '%s', which is handled by mapper '%s' and does not load items of that type.  Did you mean to use a polymorphic mapper for this relationship ?  Set 'enable_typechecks=False' on the relation() to disable this exception.  Mismatched typeloading may cause bi-directional relationships (backrefs) to not function properly." % (child.__class__, self.prop, self.mapper))
         
     def _synchronize(self, obj, child, associationrow, clearkeys, uowcommit):
         """Called during a flush to synchronize primary key identifier
index 95f6c1b3b95f0d48aba04dee5ed9ff50bb0bef86..2d10b2f9db9a1f1a148b5cad12977c078dd86254 100644 (file)
@@ -62,7 +62,7 @@ class PropertyLoader(StrategizedProperty):
     of items that correspond to a related database table.
     """
 
-    def __init__(self, argument, secondary, primaryjoin, secondaryjoin, foreign_keys=None, foreignkey=None, uselist=None, private=False, association=None, order_by=False, attributeext=None, backref=None, is_backref=False, post_update=False, cascade=None, viewonly=False, lazy=True, collection_class=None, passive_deletes=False, remote_side=None):
+    def __init__(self, argument, secondary, primaryjoin, secondaryjoin, foreign_keys=None, foreignkey=None, uselist=None, private=False, association=None, order_by=False, attributeext=None, backref=None, is_backref=False, post_update=False, cascade=None, viewonly=False, lazy=True, collection_class=None, passive_deletes=False, remote_side=None, enable_typechecks=True):
         self.uselist = uselist
         self.argument = argument
         self.secondary = secondary
@@ -77,6 +77,7 @@ class PropertyLoader(StrategizedProperty):
         self.collection_class = collection_class
         self.passive_deletes = passive_deletes
         self.remote_side = util.to_set(remote_side)
+        self.enable_typechecks = enable_typechecks
         self._parent_join_cache = {}
 
         if cascade is not None:
index 5012b5e4d15ef0b3a1cef8746eae611dbfc9c9b1..66ad3fae61f65540b4fa5c03c520fefc7515c2a7 100644 (file)
@@ -614,7 +614,7 @@ class TypeMatchTest(testbase.ORMTest):
             sess.flush()
             assert False
         except exceptions.FlushError, err:
-            assert str(err) == "Attempting to flush an item of type %s on collection 'A.bs (B)', which is handled by mapper 'Mapper|B|b' and does not load items of that type.  Did you mean to use a polymorphic mapper for this relationship ?" % C
+            assert str(err).startswith("Attempting to flush an item of type %s on collection 'A.bs (B)', which is handled by mapper 'Mapper|B|b' and does not load items of that type.  Did you mean to use a polymorphic mapper for this relationship ?" % C)
     def test_o2m_nopoly_onflush(self):
         class A(object):pass
         class B(object):pass
@@ -636,7 +636,7 @@ class TypeMatchTest(testbase.ORMTest):
             sess.flush()
             assert False
         except exceptions.FlushError, err:
-            assert str(err) == "Attempting to flush an item of type %s on collection 'A.bs (B)', which is handled by mapper 'Mapper|B|b' and does not load items of that type.  Did you mean to use a polymorphic mapper for this relationship ?" % C
+            assert str(err).startswith("Attempting to flush an item of type %s on collection 'A.bs (B)', which is handled by mapper 'Mapper|B|b' and does not load items of that type.  Did you mean to use a polymorphic mapper for this relationship ?" % C)
     
     def test_m2o_nopoly_onflush(self):
         class A(object):pass
@@ -655,7 +655,7 @@ class TypeMatchTest(testbase.ORMTest):
             sess.flush()
             assert False
         except exceptions.FlushError, err:
-            assert str(err) == "Attempting to flush an item of type %s on collection 'D.a (A)', which is handled by mapper 'Mapper|A|a' and does not load items of that type.  Did you mean to use a polymorphic mapper for this relationship ?" % B
+            assert str(err).startswith("Attempting to flush an item of type %s on collection 'D.a (A)', which is handled by mapper 'Mapper|A|a' and does not load items of that type.  Did you mean to use a polymorphic mapper for this relationship ?" % B)
     def test_m2o_oncascade(self):
         class A(object):pass
         class B(object):pass