]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commit
Disable and disallow Result.unique() with yield_per
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 6 Apr 2021 20:47:00 +0000 (16:47 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 6 Apr 2021 21:12:35 +0000 (17:12 -0400)
commit5ef1b89d865679fa2ca4bb3e3c1892bdd966ad89
treec0d7d9199e696a1c504dc6c2e9156f3d6369d0a9
parentd5a22410474f51170f18958a623e4f6c05e6b47e
Disable and disallow Result.unique() with yield_per

Fixed critical regression where the :meth:`_orm.Query.yield_per` method in
the ORM would set up the internal :class:`_engine.Result` to yield chunks
at a time, however made use of the new :meth:`_engine.Result.unique` method
which uniques across the entire result. This would lead to lost rows since
the ORM is using ``id(obj)`` as the uniquing function, which leads to
repeated identifiers for new objects as already-seen objects are garbage
collected. 1.3's behavior here was to "unique" across each chunk, which
does not actually produce "uniqued" results when results are yielded in
chunks. As the :meth:`_orm.Query.yield_per` method is already explicitly
disallowed when joined eager loading is in place, which is the primary
rationale for the "uniquing" feature, the "uniquing" feature is now turned
off entirely when :meth:`_orm.Query.yield_per` is used.

This regression only applies to the legacy :class:`_orm.Query` object; when
using :term:`2.0 style` execution, "uniquing" is not automatically applied.
To prevent the issue from arising from explicit use of
:meth:`_engine.Result.unique`, an error is now raised if rows are fetched
from a "uniqued" ORM-level :class:`_engine.Result` if any
:ref:`yield per <orm_queryguide_yield_per>` API is also in use, as the
purpose of ``yield_per`` is to allow for arbitrarily large numbers of rows,
which cannot be uniqued in memory without growing the number of entries to
fit the complete result size.

Fixes: #6206
Change-Id: I3770d1f2e9be44d82c83ca992afb912dcc17af05
doc/build/changelog/unreleased_14/6206.rst [new file with mode: 0644]
doc/build/orm/queryguide.rst
lib/sqlalchemy/orm/loading.py
lib/sqlalchemy/orm/query.py
test/orm/test_query.py