]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Error message when a string arg sent to :func:`.relationship` which
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 12 Dec 2013 17:49:57 +0000 (12:49 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 12 Dec 2013 17:53:34 +0000 (12:53 -0500)
doesn't resolve to a class or mapper has been corrected to work
the same way as when a non-string arg is received, which indicates
the name of the relationship which had the configurational error.
[ticket:2888]

Conflicts:
lib/sqlalchemy/orm/relationships.py

doc/build/changelog/changelog_08.rst
lib/sqlalchemy/orm/properties.py
test/ext/declarative/test_basic.py

index 7fd698ad6622a3545eaa87b5ef5e5c1ae0d2fea3..b65dd77cd98810be38b2ddf53e5c60f5f69a55b8 100644 (file)
@@ -8,6 +8,19 @@
     .. include:: changelog_07.rst
         :start-line: 5
 
+.. changelog::
+    :version: 0.8.5
+
+    .. change::
+        :tags: bug, declarative
+        :versions: 0.9.0b2
+        :tickets: 2888
+
+        Error message when a string arg sent to :func:`.relationship` which
+        doesn't resolve to a class or mapper has been corrected to work
+        the same way as when a non-string arg is received, which indicates
+        the name of the relationship which had the configurational error.
+
 .. changelog::
     :version: 0.8.4
     :released: December 8, 2013
index f832091c0e4bda4822e7435f35aba605a072e96c..da751404b8f3fa1152bf011488cfc64daf617874 100644 (file)
@@ -997,23 +997,23 @@ class RelationshipProperty(StrategizedProperty):
         This is a lazy-initializing static attribute.
 
         """
-        if isinstance(self.argument, type):
-            mapper_ = mapper.class_mapper(self.argument,
-                    configure=False)
-        elif isinstance(self.argument, mapper.Mapper):
-            mapper_ = self.argument
-        elif util.callable(self.argument):
-
-            # accept a callable to suit various deferred-
-            # configurational schemes
-
-            mapper_ = mapper.class_mapper(self.argument(),
-                    configure=False)
+
+        if util.callable(self.argument) and \
+            not isinstance(self.argument, (type, mapperlib.Mapper)):
+            argument = self.argument()
+        else:
+            argument = self.argument
+
+        if isinstance(argument, type):
+            mapper_ = mapperlib.class_mapper(argument,
+                     configure=False)
+        elif isinstance(self.argument, mapperlib.Mapper):
+            mapper_ = argument
+
         else:
             raise sa_exc.ArgumentError("relationship '%s' expects "
                     "a class or a mapper argument (received: %s)"
-                    % (self.key, type(self.argument)))
-        assert isinstance(mapper_, mapper.Mapper), mapper_
+                    % (self.key, type(argument)))
         return mapper_
 
     @util.memoized_property
index 4b5736c9f2a7030f0eaa9115ec14d58adba42961..1596b78d354a71179f7e7689dd36a55637b11ec6 100644 (file)
@@ -123,6 +123,38 @@ class DeclarativeTest(DeclarativeTestBase):
         assert class_mapper(Bar).get_property('some_data').columns[0] \
             is t.c.data
 
+    def test_relationship_level_msg_for_invalid_callable(self):
+        class A(Base):
+            __tablename__ = 'a'
+            id = Column(Integer, primary_key=True)
+        class B(Base):
+            __tablename__ = 'b'
+            id = Column(Integer, primary_key=True)
+            a_id = Column(Integer, ForeignKey('a.id'))
+            a = relationship('a')
+        assert_raises_message(
+            sa.exc.ArgumentError,
+            "relationship 'a' expects a class or a mapper "
+            "argument .received: .*Table",
+            configure_mappers
+        )
+
+    def test_relationship_level_msg_for_invalid_object(self):
+        class A(Base):
+            __tablename__ = 'a'
+            id = Column(Integer, primary_key=True)
+        class B(Base):
+            __tablename__ = 'b'
+            id = Column(Integer, primary_key=True)
+            a_id = Column(Integer, ForeignKey('a.id'))
+            a = relationship(A.__table__)
+        assert_raises_message(
+            sa.exc.ArgumentError,
+            "relationship 'a' expects a class or a mapper "
+            "argument .received: .*Table",
+            configure_mappers
+        )
+
     def test_difficult_class(self):
         """test no getattr() errors with a customized class"""