From: Mike Bayer Date: Thu, 21 Apr 2022 17:27:16 +0000 (-0400) Subject: fix result.columns() method X-Git-Tag: rel_2_0_0b1~337 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fe2045fb1c767436ed1e32359fe005dabead504a;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git fix result.columns() method 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 --- diff --git a/doc/build/changelog/unreleased_14/7953.rst b/doc/build/changelog/unreleased_14/7953.rst new file mode 100644 index 0000000000..0f2fd39363 --- /dev/null +++ b/doc/build/changelog/unreleased_14/7953.rst @@ -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. diff --git a/lib/sqlalchemy/engine/result.py b/lib/sqlalchemy/engine/result.py index 11998e7188..71320a583d 100644 --- a/lib/sqlalchemy/engine/result.py +++ b/lib/sqlalchemy/engine/result.py @@ -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 diff --git a/test/base/test_result.py b/test/base/test_result.py index 7a696d352a..bc7bfefa49 100644 --- a/test/base/test_result.py +++ b/test/base/test_result.py @@ -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"])