]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Raise NotImplemenedError for association proxy __clause_element__
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 27 Aug 2020 19:50:47 +0000 (15:50 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 27 Aug 2020 19:53:01 +0000 (15:53 -0400)
It's not possible right now to use an association proxy element as a plain
column expression to be SELECTed from or used in a SQL function.  An
informative error is now raised when this occurs.

Fixes: #5542
Change-Id: I334e767ebc0b56c1dccc4a1e5185b0435af77b93

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

diff --git a/doc/build/changelog/unreleased_13/5541.rst b/doc/build/changelog/unreleased_13/5541.rst
new file mode 100644 (file)
index 0000000..73bb4b6
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, ext, associationproxy
+    :tickets: 5541, 5542
+
+    It's not possible right now to use an association proxy element as a plain
+    column expression to be SELECTed from or used in a SQL function.  An
+    informative error is now raised when this occurs.
+
index dd6217664a76e8080b2b12d4418b76f44db22d02..3ea77a952e1f8f33ff48fb7e9c9ee4662d25f579 100644 (file)
@@ -421,6 +421,12 @@ class AssociationProxyInstance(object):
     def _comparator(self):
         return self._get_property().comparator
 
+    def __clause_element__(self):
+        raise NotImplementedError(
+            "The association proxy can't be used as a plain column "
+            "expression; it only works inside of a comparison expression"
+        )
+
     @classmethod
     def _cls_unwrap_target_assoc_proxy(cls, target_class, value_attr):
         attr = getattr(target_class, value_attr)
index 6fdcf3e1165a357c3815ee404e8f897c3667d747..77d81b4f7b965d199896cc6eed19b6bab3c8ac98 100644 (file)
@@ -4,6 +4,7 @@ import pickle
 from sqlalchemy import cast
 from sqlalchemy import exc
 from sqlalchemy import ForeignKey
+from sqlalchemy import func
 from sqlalchemy import inspect
 from sqlalchemy import Integer
 from sqlalchemy import MetaData
@@ -1667,6 +1668,23 @@ class ComparatorTest(fixtures.MappedTest, AssertsCompiledSQL):
         eq_(str(proxy_sql), str(direct_sql))
         eq_(q_proxy.all(), q_direct.all())
 
+    def test_no_straight_expr(self):
+        User = self.classes.User
+
+        assert_raises_message(
+            NotImplementedError,
+            "The association proxy can't be used as a plain column expression",
+            func.foo,
+            User.singular_value,
+        )
+
+        assert_raises_message(
+            NotImplementedError,
+            "The association proxy can't be used as a plain column expression",
+            self.session.query,
+            User.singular_value,
+        )
+
     def test_filter_any_criterion_ul_scalar(self):
         UserKeyword, User = self.classes.UserKeyword, self.classes.User