From: Mike Bayer Date: Tue, 6 Mar 2007 16:47:43 +0000 (+0000) Subject: added "enable_typechecks=True" flag on relation so the new type check from #500 can... X-Git-Tag: rel_0_3_6~39 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7b43cd5092436150a4c9648431173c9e79356090;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git added "enable_typechecks=True" flag on relation so the new type check from #500 can be disabled, since people are going to want to disable it. --- diff --git a/lib/sqlalchemy/orm/dependency.py b/lib/sqlalchemy/orm/dependency.py index 0b3b70d219..a528c13274 100644 --- a/lib/sqlalchemy/orm/dependency.py +++ b/lib/sqlalchemy/orm/dependency.py @@ -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 diff --git a/lib/sqlalchemy/orm/properties.py b/lib/sqlalchemy/orm/properties.py index 95f6c1b3b9..2d10b2f9db 100644 --- a/lib/sqlalchemy/orm/properties.py +++ b/lib/sqlalchemy/orm/properties.py @@ -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: diff --git a/test/orm/relationships.py b/test/orm/relationships.py index 5012b5e4d1..66ad3fae61 100644 --- a/test/orm/relationships.py +++ b/test/orm/relationships.py @@ -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