From: Mike Bayer Date: Fri, 29 Jan 2010 18:55:03 +0000 (+0000) Subject: for string deferred evals, don't return the underlying Column, rely upon the original... X-Git-Tag: rel_0_6beta1~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5935b9e367da514f5ca627381053a28b6080006b;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git for string deferred evals, don't return the underlying Column, rely upon the original propcomparator to do what it wants. reduces duplication of the "columns[0]" rule and removes potentially surprise behavior from the eval --- diff --git a/lib/sqlalchemy/ext/declarative.py b/lib/sqlalchemy/ext/declarative.py index 816bcf3e87..234aac17ca 100644 --- a/lib/sqlalchemy/ext/declarative.py +++ b/lib/sqlalchemy/ext/declarative.py @@ -585,14 +585,16 @@ class _GetColumns(object): def __init__(self, cls): self.cls = cls def __getattr__(self, key): + mapper = class_mapper(self.cls, compile=False) - if not mapper: - return getattr(self.cls, key) - else: + if mapper: 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] + raise exceptions.InvalidRequestError( + "Property %r is not an instance of" + " ColumnProperty (i.e. does not correspond" + " directly to a Column)." % key) + return getattr(self.cls, key) def _deferred_relation(cls, prop): diff --git a/test/ext/test_declarative.py b/test/ext/test_declarative.py index cf5f72050f..52cea62c4f 100644 --- a/test/ext/test_declarative.py +++ b/test/ext/test_declarative.py @@ -156,6 +156,25 @@ class DeclarativeTest(DeclarativeTestBase): rel = relation("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_no_magic(self): + """test that full tinkery expressions work as written""" + + class User(Base, ComparableEntity): + __tablename__ = 'users' + id = Column(Integer, primary_key=True) + addresses = relation("Address", + primaryjoin="User.id==Address.user_id.prop.columns[0]") + + class Address(Base, ComparableEntity): + __tablename__ = 'addresses' + id = Column(Integer, primary_key=True) + user_id = Column(Integer, ForeignKey('users.id')) + + compile_mappers() + eq_( + str(User.addresses.prop.primaryjoin), "users.id = addresses.user_id" + ) + def test_string_dependency_resolution_in_backref(self): class User(Base, ComparableEntity): __tablename__ = 'users'