Mike Bayer committed 69e24a5

descriptive error message raised when string-based relation() expressions inadvertently mistake a PropertyLoader for a ColumnLoader property

Comments (0)

Files changed (2)


         if not mapper:
             return getattr(self.cls, key)
-            return mapper.get_property(key).columns[0]
+            prop = mapper.get_property(key)
+            if not isinstance(prop, ColumnProperty):
+                raise exceptions.InvalidRequestError("Property %r is not an instance of ColumnProperty (i.e. does not correspnd directly to a Column)." % key)
+            return prop.columns[0]
 def _deferred_relation(cls, prop):


 import testenv; testenv.configure_for_tests()
 from sqlalchemy.ext import declarative as decl
+from sqlalchemy import exc
 from testlib import sa, testing
 from import MetaData, Table, Column, Integer, String, ForeignKey, ForeignKeyConstraint, asc
-from import relation, create_session, class_mapper, eagerload
+from import relation, create_session, class_mapper, eagerload, compile_mappers
 from testlib.testing import eq_
 from orm._base import ComparableEntity
         self.assertEquals(sess.query(User).filter( == 'ed').one(),
             User(name='ed', addresses=[Address(email='xyz'), Address(email='def'), Address(email='abc')])
+        class Foo(Base, ComparableEntity):
+            __tablename__ = 'foo'
+            id = Column(Integer, primary_key=True)
+            rel = relation("User", primaryjoin="")
+        self.assertRaisesMessage(exc.InvalidRequestError, "'addresses' is not an instance of ColumnProperty", compile_mappers)
     def test_uncompiled_attributes_in_relation(self):
         class Address(Base, ComparableEntity):