]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Restore subquery.as_scalar() w/ deprecation
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 10 Sep 2019 16:46:21 +0000 (12:46 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 11 Sep 2019 05:57:47 +0000 (01:57 -0400)
Apparently Alias had an .as_scalar() method, so restore an
equivalent to Subquery with an appropriate deprecation warning.

Fixes: #4854
Change-Id: I6255d61b7d82487ca90ba8ee79d4b3a74e7cbe38

doc/build/changelog/unreleased_14/4617_scalar.rst
lib/sqlalchemy/sql/selectable.py
test/sql/test_deprecations.py

index d47b3e464ad8084a56e25c6aae6492fde42be411..6fb4adcca719c6cf7b066865a1d798776131c600 100644 (file)
     This warning will in a later major release become an error, however the
     message will always be clear when :meth:`.SelectBase.scalar_subquery` needs
     to be invoked.   The latter part of the change is for clarity and to reduce
-    the implicit decisionmaking by the query coercion system.
+    the implicit decisionmaking by the query coercion system.   The
+    :meth:`.Subquery.as_scalar` method, which was previously
+    ``Alias.as_scalar``, is also deprecated; ``.scalar_subquery()`` should be
+    invoked directly from ` :func:`.select` object or :class:`.Query` object.
 
     This change is part of the larger change to convert :func:`.select` objects
     to no longer be directly part of the "from clause" class hierarchy, which
index 00d3826b21f2a4b9660b105685fc1343ccd8c1c6..166e592b628d079f0791b07548b7556893c605ec 100644 (file)
@@ -1753,6 +1753,18 @@ class Subquery(AliasedReturnsRows):
             roles.SelectStatementRole, selectable
         ).subquery(name=name)
 
+    @util.deprecated(
+        "1.4",
+        "The :meth:`.Subquery.as_scalar` method, which was previously "
+        "``Alias.as_scalar()`` prior to version 1.4, is deprecated and "
+        "will be removed in a future release; Please use the "
+        ":meth:`.Select.scalar_subquery` method of the :func:`.select` "
+        "construct before constructing a subquery object, or with the ORM "
+        "use the :meth:`.Query.scalar_subquery` method.",
+    )
+    def as_scalar(self):
+        return self.element.scalar_subquery()
+
 
 class FromGrouping(GroupedElement, FromClause):
     """Represent a grouping of a FROM clause"""
index d126912a5d5426ac83854e5a0ef94f81f60057e5..09deb1294497c58dba2d553a57611b3039bcbd2f 100644 (file)
@@ -579,6 +579,15 @@ class SubqueryCoercionsTest(fixtures.TestBase, AssertsCompiledSQL):
 
         is_true(stmt.compare(select([self.table1.c.myid]).scalar_subquery()))
 
+    def test_as_scalar_from_subquery(self):
+        with testing.expect_deprecated(
+            r"The Subquery.as_scalar\(\) method, which was previously "
+            r"``Alias.as_scalar\(\)`` prior to version 1.4"
+        ):
+            stmt = select([self.table1.c.myid]).subquery().as_scalar()
+
+        is_true(stmt.compare(select([self.table1.c.myid]).scalar_subquery()))
+
     def test_fromclause_subquery(self):
         stmt = select([self.table1.c.myid])
         with testing.expect_deprecated(