]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commit
Raise if unique() not applied to 2.0 joined eager load results
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 21 Sep 2020 21:28:03 +0000 (17:28 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 21 Sep 2020 23:55:59 +0000 (19:55 -0400)
commit0d1efeec475621b5c2c2aca0632b02edef54c1a6
treebe5b14aad521f8e41cd079c0e255087bbd04ea91
parent73ab000007bd25ac86ca2081868615c6c4820531
Raise if unique() not applied to 2.0 joined eager load results

The automatic uniquing of rows on the client side is turned off for the new
:term:`2.0 style` of ORM querying.  This improves both clarity and
performance.  However, uniquing of rows on the client side is generally
necessary when using joined eager loading for collections, as there
will be duplicates of the primary entity for each element in the
collection because a join was used.  This uniquing must now be manually
enabled and can be achieved using the new
:meth:`_engine.Result.unique` modifier.   To avoid silent failure, the ORM
explicitly requires the method be called when the result of an ORM
query in 2.0 style makes use of joined load collections.    The newer
:func:`_orm.selectinload` strategy is likely preferable for eager loading
of collections in any case.

This changeset also fixes an issue where ORM-style "single entity"
results would not apply unique() correctly if results were returned
as tuples.

Fixes: #4395
Change-Id: Ie62e0cb68ef2a6d2120e968b79575a70d057212e
doc/build/changelog/migration_20.rst
doc/build/changelog/unreleased_14/4395.rst [new file with mode: 0644]
lib/sqlalchemy/engine/result.py
lib/sqlalchemy/orm/loading.py
test/aaa_profiling/test_orm.py
test/base/test_result.py
test/orm/test_eager_relations.py
test/orm/test_query.py
test/orm/test_relationship_criteria.py