1. idank
  2. sqlalchemy


Mike Bayer  committed 69e24a5

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

  • Participants
  • Parent commits 7ba4346
  • Branches default

Comments (0)

Files changed (2)

File lib/sqlalchemy/ext/declarative.py

View file
         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):

File test/ext/declarative.py

View file
 import testenv; testenv.configure_for_tests()
 from sqlalchemy.ext import declarative as decl
+from sqlalchemy import exc
 from testlib import sa, testing
 from testlib.sa import MetaData, Table, Column, Integer, String, ForeignKey, ForeignKeyConstraint, asc
-from testlib.sa.orm import relation, create_session, class_mapper, eagerload
+from testlib.sa.orm 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(User.name == '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="User.addresses==Foo.id")
+        self.assertRaisesMessage(exc.InvalidRequestError, "'addresses' is not an instance of ColumnProperty", compile_mappers)
     def test_uncompiled_attributes_in_relation(self):
         class Address(Base, ComparableEntity):