]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commit
disable polymorphic adaption in most cases
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 5 Dec 2022 21:11:59 +0000 (16:11 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 7 Dec 2022 22:35:26 +0000 (17:35 -0500)
commit66c6b8558a6b64820b790199816acc66deffdacc
tree0e1c929fa8615d5042fad2a402c941b40d10b619
parent59f5beff1928e752b33d65a541cd68295ae0a5f1
disable polymorphic adaption in most cases

Improved a fix first made in version 1.4 for :ticket:`8456` which scaled
back the usage of internal "polymorphic adapters", that are used to render
ORM queries when the :paramref:`_orm.Mapper.with_polymorphic` parameter is
used. These adapters, which are very complex and error prone, are now used
only in those cases where an explicit user-supplied subquery is used for
:paramref:`_orm.Mapper.with_polymorphic`, which includes only the use case
of concrete inheritance mappings that use the
:func:`_orm.polymorphic_union` helper, as well as the legacy use case of
using an aliased subquery for joined inheritance mappings, which is not
needed in modern use.

For the most common case of joined inheritance mappings that use the
built-in polymorphic loading scheme, which includes those which make use of
the :paramref:`_orm.Mapper.polymorphic_load` parameter set to ``inline``,
polymorphic adapters are now no longer used. This has both a positive
performance impact on the construction of queries as well as a
substantial simplification of the internal query rendering process.

The specific issue targeted was to allow a :func:`_orm.column_property`
to refer to joined-inheritance classes within a scalar subquery, which now
works as intuitively as is feasible.

ORM context, mapper, strategies now use ORMAdapter in all cases
instead of straight ColumnAdapter; added some more parameters
to ORMAdapter to make this possible.  ORMAdapter now includes a
"trace" enumeration that identifies the use path for the
adapter and can aid in debugging.

implement __slots__ for the ExternalTraversal hierarchy up
to ORMAdapter.  Within this change, we have to change the
ClauseAdapter.wrap() method, which is only used in one polymorphic
codepath, to use copy.copy() instead of
`__dict__` access (apparently `__reduce_ex__` is implemented for
objects with `__slots__`), and we also remove pickling ability,
which should not be needed for adapters (this might have been needed
for 1.3 and earlier in order for Query to be picklable, but none
of that state is present within Query / select() / etc. anymore).

Fixes: #8168
Change-Id: I3f6593eb02ab5e5964807c53a9fa4894c826d017
doc/build/changelog/unreleased_20/8168.rst [new file with mode: 0644]
lib/sqlalchemy/orm/context.py
lib/sqlalchemy/orm/mapper.py
lib/sqlalchemy/orm/strategies.py
lib/sqlalchemy/orm/util.py
lib/sqlalchemy/sql/util.py
lib/sqlalchemy/sql/visitors.py
test/orm/inheritance/test_assorted_poly.py
tools/trace_orm_adapter.py [new file with mode: 0644]