From: Mike Bayer Date: Sun, 11 May 2014 03:26:09 +0000 (-0400) Subject: - Fixed ORM bug where the :func:`.class_mapper` function would mask X-Git-Tag: rel_0_8_7~33 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ac68e85e544d9e9e5905b212a1dc92c61785dda1;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Fixed ORM bug where the :func:`.class_mapper` function would mask AttributeErrors or KeyErrors that should raise during mapper configuration due to user errors. The catch for attribute/keyerror has been made more specific to not include the configuration step. fixes #3047 --- diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst index a9d6cc2dab..cfbea455c4 100644 --- a/doc/build/changelog/changelog_08.rst +++ b/doc/build/changelog/changelog_08.rst @@ -11,6 +11,16 @@ .. changelog:: :version: 0.8.7 + .. change:: + :tags: bug, orm + :tickets: 3047 + :versions: 0.9.5 + + Fixed ORM bug where the :func:`.class_mapper` function would mask + AttributeErrors or KeyErrors that should raise during mapper + configuration due to user errors. The catch for attribute/keyerror + has been made more specific to not include the configuration step. + .. change:: :tags: bug, sql :tickets: 3045 diff --git a/lib/sqlalchemy/orm/util.py b/lib/sqlalchemy/orm/util.py index 3a39232a2c..983ecd57d6 100644 --- a/lib/sqlalchemy/orm/util.py +++ b/lib/sqlalchemy/orm/util.py @@ -1075,13 +1075,13 @@ def _inspect_mapped_class(class_, configure=False): if not class_manager.is_mapped: return None mapper = class_manager.mapper + except exc.NO_STATE: + return None + else: if configure and mapperlib.module._new_mappers: mapperlib.configure_mappers() return mapper - except exc.NO_STATE: - return None - def object_mapper(instance): """Given an object, return the primary Mapper associated with the object diff --git a/test/orm/test_mapper.py b/test/orm/test_mapper.py index 0ca3480dd3..8d10a836b8 100644 --- a/test/orm/test_mapper.py +++ b/test/orm/test_mapper.py @@ -1579,6 +1579,41 @@ class MapperTest(_fixtures.FixtureTest, AssertsCompiledSQL): class_mapper, (5, 6) ) + def test_attribute_error_raised_class_mapper(self): + users = self.tables.users + addresses = self.tables.addresses + User = self.classes.User + Address = self.classes.Address + + mapper(User, users, properties={ + "addresses": relationship(Address, + primaryjoin=lambda: users.c.id == addresses.wrong.user_id) + }) + mapper(Address, addresses) + assert_raises_message( + AttributeError, + "'Table' object has no attribute 'wrong'", + class_mapper, Address + ) + + def test_key_error_raised_class_mapper(self): + users = self.tables.users + addresses = self.tables.addresses + User = self.classes.User + Address = self.classes.Address + + mapper(User, users, properties={ + "addresses": relationship(Address, + primaryjoin=lambda: users.c.id == + addresses.__dict__['wrong'].user_id) + }) + mapper(Address, addresses) + assert_raises_message( + KeyError, + "wrong", + class_mapper, Address + ) + def test_unmapped_subclass_error_postmap(self): users = self.tables.users