diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2013-12-12 12:49:57 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2013-12-12 12:49:57 -0500 |
commit | ace7bdbc78571619cb103d57a188fbe6aa92b627 (patch) | |
tree | daa52736c7bcd396894d405d05374550d0693783 | |
parent | 9bef2001a604b520a82abc65cf95790211d36106 (diff) | |
download | sqlalchemy-ace7bdbc78571619cb103d57a188fbe6aa92b627.tar.gz |
- 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.
[ticket:2888]
-rw-r--r-- | doc/build/changelog/changelog_08.rst | 13 | ||||
-rw-r--r-- | lib/sqlalchemy/orm/relationships.py | 22 | ||||
-rw-r--r-- | test/ext/declarative/test_basic.py | 32 |
3 files changed, 55 insertions, 12 deletions
diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst index 0902ff7e4..2cd9b5855 100644 --- a/doc/build/changelog/changelog_08.rst +++ b/doc/build/changelog/changelog_08.rst @@ -9,6 +9,19 @@ :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 .. change:: diff --git a/lib/sqlalchemy/orm/relationships.py b/lib/sqlalchemy/orm/relationships.py index 8296be60a..c1cf07fbe 100644 --- a/lib/sqlalchemy/orm/relationships.py +++ b/lib/sqlalchemy/orm/relationships.py @@ -1348,23 +1348,21 @@ class RelationshipProperty(StrategizedProperty): This is a lazy-initializing static attribute. """ - if isinstance(self.argument, type): - mapper_ = mapperlib.class_mapper(self.argument, - configure=False) - elif isinstance(self.argument, mapperlib.Mapper): - mapper_ = self.argument - elif util.callable(self.argument): - - # accept a callable to suit various deferred- - # configurational schemes + if util.callable(self.argument) and \ + not isinstance(self.argument, (type, mapperlib.Mapper)): + argument = self.argument() + else: + argument = self.argument - mapper_ = mapperlib.class_mapper(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_, mapperlib.Mapper), mapper_ + % (self.key, type(argument))) return mapper_ @util.memoized_property diff --git a/test/ext/declarative/test_basic.py b/test/ext/declarative/test_basic.py index f5e7f2aa7..b119e356f 100644 --- a/test/ext/declarative/test_basic.py +++ b/test/ext/declarative/test_basic.py @@ -143,6 +143,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""" |