]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Declarative will raise an informative error message
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 2 Apr 2010 16:42:54 +0000 (12:42 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 2 Apr 2010 16:42:54 +0000 (12:42 -0400)
if a non-mapped class attribute is referenced in the
string-based relationship() arguments.

CHANGES
lib/sqlalchemy/ext/declarative.py
test/ext/test_declarative.py

diff --git a/CHANGES b/CHANGES
index f22905f267b5086fb803119c3de90e4d70cce745..fc217179c6eb07cbc36171a9ab2b48a1fb795053 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -49,6 +49,10 @@ CHANGES
      decorators on child classes that aren't broken by a 
      @compiles decorator on the base class.
    
+   - Declarative will raise an informative error message
+     if a non-mapped class attribute is referenced in the
+     string-based relationship() arguments.
+     
 0.6beta3
 ========
 
index 1f4658b605d1d67f70cac35441ee6f0124894c8e..407de100463c6c6a739be8727480ec1cbab1a569 100644 (file)
@@ -749,8 +749,12 @@ class _GetColumns(object):
         
         mapper = class_mapper(self.cls, compile=False)
         if mapper:
-            prop = mapper.get_property(key)
-            if not isinstance(prop, ColumnProperty):
+            prop = mapper.get_property(key, raiseerr=False)
+            if prop is None:
+                raise exceptions.InvalidRequestError(
+                                        "Class %r does not have a mapped column named %r"
+                                        % (self.cls, key))
+            elif not isinstance(prop, ColumnProperty):
                 raise exceptions.InvalidRequestError(
                                         "Property %r is not an instance of"
                                         " ColumnProperty (i.e. does not correspond"
index 01ce7a6357263b9a49c23181595aac469a5c0db5..d5d837da7f8f03b36a88f428649610edac44c96c 100644 (file)
@@ -187,6 +187,23 @@ class DeclarativeTest(DeclarativeTestBase):
             rel = relationship("User", primaryjoin="User.addresses==Foo.id")
         assert_raises_message(exc.InvalidRequestError, "'addresses' is not an instance of ColumnProperty", compile_mappers)
 
+    def test_string_dependency_resolution_two(self):
+        class User(Base, ComparableEntity):
+            __tablename__ = 'users'
+            id = Column(Integer, primary_key=True, test_needs_autoincrement=True)
+            name = Column(String(50))
+            addresses = relationship("Address", order_by="desc(Address.email)", 
+                primaryjoin="User.id==Address.user_id", foreign_keys="[Address.user_id]",
+                backref=backref('user', primaryjoin="User.id==Address.user_id", foreign_keys="[Address.user_id]")
+                )
+        
+        
+        class Bar(Base, ComparableEntity):
+            __tablename__ = 'bar'
+            id = Column(Integer, primary_key=True)
+            rel = relationship("User", primaryjoin="User.id==Bar.__table__.id")
+        assert_raises_message(exc.InvalidRequestError, "does not have a mapped column named '__table__'", compile_mappers)
+
     def test_string_dependency_resolution_no_magic(self):
         """test that full tinkery expressions work as written"""