]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
fix result.columns() method
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 21 Apr 2022 17:27:16 +0000 (13:27 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 21 Apr 2022 17:27:16 +0000 (13:27 -0400)
Fixed issue in :meth:`.Result.columns` method where calling upon
:meth:`.Result.columns` with a single index could in some cases,
particularly ORM result object cases, cause the :class:`.Result` to yield
scalar objects rather than :class:`.Row` objects, as though the
:meth:`.Result.scalars` method had been called. In SQLAlchemy 1.4, this
scenario emits a warning that the behavior will change in SQLAlchemy 2.0.

Fixes: #7953
Change-Id: I3c4ca3eecc2bfc85ad1c38000e5990d6dde80d22

doc/build/changelog/unreleased_14/7953.rst [new file with mode: 0644]
lib/sqlalchemy/engine/result.py
test/base/test_result.py

diff --git a/doc/build/changelog/unreleased_14/7953.rst b/doc/build/changelog/unreleased_14/7953.rst
new file mode 100644 (file)
index 0000000..0f2fd39
--- /dev/null
@@ -0,0 +1,10 @@
+.. change::
+    :tags: bug, engine
+    :tickets: 7953
+
+    Fixed issue in :meth:`.Result.columns` method where calling upon
+    :meth:`.Result.columns` with a single index could in some cases,
+    particularly ORM result object cases, cause the :class:`.Result` to yield
+    scalar objects rather than :class:`.Row` objects, as though the
+    :meth:`.Result.scalars` method had been called. In SQLAlchemy 1.4, this
+    scenario emits a warning that the behavior will change in SQLAlchemy 2.0.
index 11998e7188dcc9c3d83645830b9e8fe8863d3bd3..71320a583dc92741df71eedf950c4b850aac82be 100644 (file)
@@ -802,12 +802,11 @@ class ResultInternal(InPlaceGenerative, Generic[_R]):
             self._real_result if self._real_result else cast(Result, self)
         )
 
-        if real_result._source_supports_scalars and len(indexes) == 1:
-            self._generate_rows = False
-        else:
-            self._generate_rows = True
+        if not real_result._source_supports_scalars or len(indexes) != 1:
             self._metadata = self._metadata._reduce(indexes)
 
+        assert self._generate_rows
+
         return self
 
     @HasMemoized.memoized_attribute
index 7a696d352afb516b44dd4ce8713fddab10408c2d..bc7bfefa491fe74d7428b4f30ca8548d36451188 100644 (file)
@@ -1069,6 +1069,34 @@ class OnlyScalarsTest(fixtures.TestBase):
             [{"a": 1}, {"a": 2}, {"a": 1}, {"a": 1}, {"a": 4}],
         )
 
+    def test_scalar_mode_columns0_plain(self, no_tuple_fixture):
+        """test #7953"""
+
+        metadata = result.SimpleResultMetaData(["a", "b", "c"])
+
+        r = result.ChunkedIteratorResult(
+            metadata, no_tuple_fixture, source_supports_scalars=True
+        )
+
+        r = r.columns(0)
+        eq_(
+            list(r),
+            [(1,), (2,), (1,), (1,), (4,)],
+        )
+
+    def test_scalar_mode_scalars0(self, no_tuple_fixture):
+        metadata = result.SimpleResultMetaData(["a", "b", "c"])
+
+        r = result.ChunkedIteratorResult(
+            metadata, no_tuple_fixture, source_supports_scalars=True
+        )
+
+        r = r.scalars(0)
+        eq_(
+            list(r),
+            [1, 2, 1, 1, 4],
+        )
+
     def test_scalar_mode_but_accessed_nonscalar_result(self, no_tuple_fixture):
         metadata = result.SimpleResultMetaData(["a", "b", "c"])