]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed ORM bug where the :func:`.class_mapper` function would mask
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 11 May 2014 03:26:09 +0000 (23:26 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 11 May 2014 03:28:58 +0000 (23:28 -0400)
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

doc/build/changelog/changelog_08.rst
lib/sqlalchemy/orm/util.py
test/orm/test_mapper.py

index a9d6cc2dab32533ee0ca78344197f7d5c42a96b8..cfbea455c481e6a7f6e44fa5e7ed47f7af4a3d89 100644 (file)
 .. 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
index 3a39232a2c5e057cc4be6a1d9c7df1de40e1717b..983ecd57d631785ab1f3a08a817bd23dd15146a1 100644 (file)
@@ -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
index 0ca3480dd35f777e9068372747b7a22a3b82df91..8d10a836b8c0e883f251294be5343d65474e4cd7 100644 (file)
@@ -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