diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-04-21 13:27:16 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-04-21 13:27:16 -0400 |
| commit | fe2045fb1c767436ed1e32359fe005dabead504a (patch) | |
| tree | 9a51e5096d1299c74db0a871b9daf9cc54a3326f | |
| parent | 6aefaaa110fc239571c043fad1c8a6a4e1de4368 (diff) | |
| download | sqlalchemy-fe2045fb1c767436ed1e32359fe005dabead504a.tar.gz | |
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
| -rw-r--r-- | doc/build/changelog/unreleased_14/7953.rst | 10 | ||||
| -rw-r--r-- | lib/sqlalchemy/engine/result.py | 7 | ||||
| -rw-r--r-- | test/base/test_result.py | 28 |
3 files changed, 41 insertions, 4 deletions
diff --git a/doc/build/changelog/unreleased_14/7953.rst b/doc/build/changelog/unreleased_14/7953.rst new file mode 100644 index 000000000..0f2fd3936 --- /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 11998e718..71320a583 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 7a696d352..bc7bfefa4 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"]) |
