]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fix subqueryload losing .and_() criteria with of_type()
authorArya Rizky <algojogacor@users.noreply.github.com>
Tue, 12 May 2026 12:45:51 +0000 (19:45 +0700)
committerArya Rizky <algojogacor@users.noreply.github.com>
Tue, 12 May 2026 12:45:51 +0000 (19:45 +0700)
Fixed issue where subqueryload would silently ignore additional filter
criteria passed via .and_() when of_type() was also used. Two changes:

1. In _SubqueryLoader._setup_options(), use effective_entity instead of
   self.entity when creating the LoaderCriteriaOption, so that the criteria
   is associated with the correct polymorphic entity (e.g. the of_type()
   target). This matches the behavior of selectinload which already uses
   effective_entity.

2. In _JoinCondition.join_targets mark_exclude_cols(), add a mapper.isa()
   check so that subclass mapper columns are recognized as belonging to
   the relationship target. Previously, only the base class mapper was
   checked via identity comparison (is not self.prop.mapper), causing
   subclass columns to be annotated with should_not_adapt, which prevented
   the ClauseAdapter from rewriting them to use the subquery alias.

The same mapper.isa() fix also resolves #13203 (joinedload variant).

Fixes: #13207
Related: #13203

lib/sqlalchemy/orm/relationships.py
lib/sqlalchemy/orm/strategies.py

index 22100e9ea6051a31fbd08c98e0ad720391d65f1d..e7ccbc4cf94def2b37cebeef2a88144db90cd33a 100644 (file)
@@ -3383,6 +3383,12 @@ class _JoinCondition:
                 if (
                     parentmapper_for_element is not self.prop.parent
                     and parentmapper_for_element is not self.prop.mapper
+                    and not (
+                        parentmapper_for_element is not None
+                        and parentmapper_for_element.isa(
+                            self.prop.mapper
+                        )
+                    )
                     and elem not in self._secondary_lineage_set
                 ):
                     return _safe_annotate(elem, annotations)
index d7672b3e4a052d2ad98a24880e9ad306be5d0663..ddcb0c814389abb144d97e504b2988aaf3b6979b 100644 (file)
@@ -1760,7 +1760,7 @@ class _SubqueryLoader(_PostLoader):
         if loadopt and loadopt._extra_criteria:
             new_options += (
                 orm_util.LoaderCriteriaOption(
-                    self.entity,
+                    effective_entity,
                     loadopt._generate_extra_criteria(context),
                 ),
             )