From a3a85ed54f0a3b99037b6ed25ac6c3c16d3b221c Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 2 Apr 2010 12:42:54 -0400 Subject: [PATCH] - Declarative will raise an informative error message if a non-mapped class attribute is referenced in the string-based relationship() arguments. --- CHANGES | 4 ++++ lib/sqlalchemy/ext/declarative.py | 8 ++++++-- test/ext/test_declarative.py | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index f22905f267..fc217179c6 100644 --- 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 ======== diff --git a/lib/sqlalchemy/ext/declarative.py b/lib/sqlalchemy/ext/declarative.py index 1f4658b605..407de10046 100644 --- a/lib/sqlalchemy/ext/declarative.py +++ b/lib/sqlalchemy/ext/declarative.py @@ -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" diff --git a/test/ext/test_declarative.py b/test/ext/test_declarative.py index 01ce7a6357..d5d837da7f 100644 --- a/test/ext/test_declarative.py +++ b/test/ext/test_declarative.py @@ -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""" -- 2.47.3