From c4506b8c906c2606b50cdf8643cf01112bf1fa01 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 11 Dec 2011 10:41:33 -0500 Subject: [PATCH] - [bug] Fixed bug whereby hybrid_property didn't work as a kw arg in any(), has(). --- CHANGES | 6 ++++ lib/sqlalchemy/orm/properties.py | 2 +- test/ext/test_hybrid.py | 47 +++++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 0a2029a3db..2b4959e907 100644 --- a/CHANGES +++ b/CHANGES @@ -3,6 +3,12 @@ ======= CHANGES ======= +0.7.5 +===== +- orm + - [bug] Fixed bug whereby hybrid_property didn't + work as a kw arg in any(), has(). + 0.7.4 ===== - orm diff --git a/lib/sqlalchemy/orm/properties.py b/lib/sqlalchemy/orm/properties.py index 8794cd3dad..40032c9497 100644 --- a/lib/sqlalchemy/orm/properties.py +++ b/lib/sqlalchemy/orm/properties.py @@ -428,7 +428,7 @@ class RelationshipProperty(StrategizedProperty): source_selectable=source_selectable) for k in kwargs: - crit = self.property.mapper.class_manager[k] == kwargs[k] + crit = getattr(self.property.mapper.class_, k) == kwargs[k] if criterion is None: criterion = crit else: diff --git a/test/ext/test_hybrid.py b/test/ext/test_hybrid.py index 649b796f72..7ed8e207c3 100644 --- a/test/ext/test_hybrid.py +++ b/test/ext/test_hybrid.py @@ -1,4 +1,4 @@ -from sqlalchemy import func, Integer, String +from sqlalchemy import func, Integer, String, ForeignKey from sqlalchemy.orm import relationship, Session, aliased from test.lib.schema import Column from sqlalchemy.ext.declarative import declarative_base @@ -23,6 +23,7 @@ class PropertyComparatorTest(fixtures.TestBase, AssertsCompiledSQL): class A(Base): __tablename__ = 'a' id = Column(Integer, primary_key=True) + _value = Column("value", String) @hybrid.hybrid_property @@ -106,6 +107,39 @@ class PropertyExpressionTest(fixtures.TestBase, AssertsCompiledSQL): return A + def _relationship_fixture(self): + Base = declarative_base() + + class A(Base): + __tablename__ = 'a' + id = Column(Integer, primary_key=True) + b_id = Column('bid', Integer, ForeignKey('b.id')) + _value = Column("value", String) + + @hybrid.hybrid_property + def value(self): + return int(self._value) - 5 + + @value.expression + def value(cls): + return func.foo(cls._value) + cls.bar_value + + @value.setter + def value(self, v): + self._value = v + 5 + + @hybrid.hybrid_property + def bar_value(cls): + return func.bar(cls._value) + + class B(Base): + __tablename__ = 'b' + id = Column(Integer, primary_key=True) + + as_ = relationship("A") + + return A, B + def test_set_get(self): A = self._fixture() a1 = A(value=5) @@ -119,6 +153,17 @@ class PropertyExpressionTest(fixtures.TestBase, AssertsCompiledSQL): "foo(a.value) + bar(a.value)" ) + def test_any(self): + A, B = self._relationship_fixture() + sess = Session() + self.assert_compile( + sess.query(B).filter(B.as_.any(value=5)), + "SELECT b.id AS b_id FROM b WHERE EXISTS " + "(SELECT 1 FROM a WHERE b.id = a.bid " + "AND foo(a.value) + bar(a.value) = :param_1)" + ) + + def test_aliased_expression(self): A = self._fixture() self.assert_compile( -- 2.47.2