]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Ensure association proxy works over synonym
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 5 Mar 2019 18:28:54 +0000 (13:28 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 6 Mar 2019 15:16:49 +0000 (10:16 -0500)
Fixed regression where an association proxy linked to a synonym would no
longer work, both at instance level and at class level.

Fixes: #4522
Change-Id: I949079229ef87c12736c362df35444f6e30c8038

doc/build/changelog/unreleased_13/4522.rst [new file with mode: 0644]
lib/sqlalchemy/orm/descriptor_props.py
test/ext/test_associationproxy.py

diff --git a/doc/build/changelog/unreleased_13/4522.rst b/doc/build/changelog/unreleased_13/4522.rst
new file mode 100644 (file)
index 0000000..cd07296
--- /dev/null
@@ -0,0 +1,6 @@
+.. change::
+   :tags: bug, orm, ext
+   :tickets: 4522
+
+   Fixed regression where an association proxy linked to a synonym would no
+   longer work, both at instance level and at class level.
index a0ddf3a8b65e039bf153cdd68b7aeb14c0df771c..28b3bc5db3b959cceb9550ac0ea98bb4ec8fd3a5 100644 (file)
@@ -39,6 +39,10 @@ class DescriptorProperty(MapperProperty):
             expire_missing = True
             collection = False
 
+            @property
+            def uses_objects(self):
+                return getattr(mapper.class_, prop.name).impl.uses_objects
+
             def __init__(self, key):
                 self.key = key
 
index 4b07a383d1f7e3f680b4c1211a48a311b4ba9e03..6f701416ccfd4351232bb272b08616ac053f8378 100644 (file)
@@ -3089,6 +3089,58 @@ class MultiOwnerTest(
         self._assert_raises_ambiguous(lambda: D.c_data.any(B.id == 5))
 
 
+class ProxyOfSynonymTest(AssertsCompiledSQL, fixtures.DeclarativeMappedTest):
+    __dialect__ = "default"
+
+    run_create_tables = None
+
+    @classmethod
+    def setup_classes(cls):
+        from sqlalchemy.orm import synonym
+
+        Base = cls.DeclarativeBasic
+
+        class A(Base):
+            __tablename__ = "a"
+
+            id = Column(Integer, primary_key=True)
+            data = Column(String)
+            bs = relationship("B", backref="a")
+            data_syn = synonym("data")
+
+            b_data = association_proxy("bs", "data_syn")
+
+        class B(Base):
+            __tablename__ = "b"
+            id = Column(Integer, primary_key=True)
+            a_id = Column(ForeignKey("a.id"))
+            data = Column(String)
+            data_syn = synonym("data")
+
+            a_data = association_proxy("a", "data_syn")
+
+    def test_o2m_instance_getter(self):
+        A, B = self.classes("A", "B")
+
+        a1 = A(bs=[B(data="bdata1"), B(data="bdata2")])
+        eq_(a1.b_data, ["bdata1", "bdata2"])
+
+    def test_m2o_instance_getter(self):
+        A, B = self.classes("A", "B")
+
+        b1 = B(a=A(data="adata"))
+        eq_(b1.a_data, "adata")
+
+    def test_o2m_expr(self):
+        A, B = self.classes("A", "B")
+
+        self.assert_compile(
+            A.b_data == "foo",
+            "EXISTS (SELECT 1 FROM a, b WHERE a.id = b.a_id "
+            "AND b.data = :data_1)",
+        )
+
+
 class ProxyAttrTest(fixtures.DeclarativeMappedTest):
     @classmethod
     def setup_classes(cls):