]> 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:26:09 +0000 (23:26 -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/base.py
test/orm/test_mapper.py

index 656858720c87b70ab22dc9e122f4ebedf9984ea3..c87c5eb6eb8c8c29590423ea812d3f7ba4fa13c9 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 e973de8972859aab4c3b2d063998edc325a6e398..e81375787ca5c264054e06358ab698dc0cc0f7de 100644 (file)
@@ -351,12 +351,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 mapper._new_mappers:
             mapper._configure_all()
         return mapper
 
-    except exc.NO_STATE:
-        return None
 
 def class_mapper(class_, configure=True):
     """Given a class, return the primary :class:`.Mapper` associated
index 78f563b7023fe284564fc4599c01e528003256bc..40891e0d8ab24582cafc92d5ad22ddc2bca1170e 100644 (file)
@@ -1590,6 +1590,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