@@ -417,7 +417,10 @@ def create_for_statement(cls, statement_container, compiler, **kw):
417417 )
418418
419419 _QueryEntity .to_compile_state (
420- self , statement_container ._raw_columns , self ._entities
420+ self ,
421+ statement_container ._raw_columns ,
422+ self ._entities ,
423+ is_current_entities = True ,
421424 )
422425
423426 self .current_path = statement_container ._compile_options ._current_path
@@ -590,14 +593,18 @@ def create_for_statement(cls, statement, compiler, **kw):
590593 self ,
591594 memoized_entities ._raw_columns ,
592595 [],
596+ is_current_entities = False ,
593597 )
594598 for memoized_entities in (
595599 select_statement ._memoized_select_entities
596600 )
597601 }
598602
599603 _QueryEntity .to_compile_state (
600- self , select_statement ._raw_columns , self ._entities
604+ self ,
605+ select_statement ._raw_columns ,
606+ self ._entities ,
607+ is_current_entities = True ,
601608 )
602609
603610 self .current_path = select_statement ._compile_options ._current_path
@@ -839,7 +846,9 @@ def _create_entities_collection(cls, query, legacy):
839846
840847 # entities will also set up polymorphic adapters for mappers
841848 # that have with_polymorphic configured
842- _QueryEntity .to_compile_state (self , query ._raw_columns , self ._entities )
849+ _QueryEntity .to_compile_state (
850+ self , query ._raw_columns , self ._entities , is_current_entities = True
851+ )
843852 return self
844853
845854 @classmethod
@@ -2278,13 +2287,18 @@ class _QueryEntity(object):
22782287 use_id_for_hash = False
22792288
22802289 @classmethod
2281- def to_compile_state (cls , compile_state , entities , entities_collection ):
2290+ def to_compile_state (
2291+ cls , compile_state , entities , entities_collection , is_current_entities
2292+ ):
22822293
22832294 for idx , entity in enumerate (entities ):
22842295 if entity ._is_lambda_element :
22852296 if entity ._is_sequence :
22862297 cls .to_compile_state (
2287- compile_state , entity ._resolved , entities_collection
2298+ compile_state ,
2299+ entity ._resolved ,
2300+ entities_collection ,
2301+ is_current_entities ,
22882302 )
22892303 continue
22902304 else :
@@ -2294,7 +2308,10 @@ def to_compile_state(cls, compile_state, entities, entities_collection):
22942308 if entity .is_selectable :
22952309 if "parententity" in entity ._annotations :
22962310 _MapperEntity (
2297- compile_state , entity , entities_collection
2311+ compile_state ,
2312+ entity ,
2313+ entities_collection ,
2314+ is_current_entities ,
22982315 )
22992316 else :
23002317 _ColumnEntity ._for_columns (
@@ -2343,12 +2360,15 @@ class _MapperEntity(_QueryEntity):
23432360 "_polymorphic_discriminator" ,
23442361 )
23452362
2346- def __init__ (self , compile_state , entity , entities_collection ):
2363+ def __init__ (
2364+ self , compile_state , entity , entities_collection , is_current_entities
2365+ ):
23472366 entities_collection .append (self )
2348- if compile_state ._primary_entity is None :
2349- compile_state ._primary_entity = self
2350- compile_state ._has_mapper_entities = True
2351- compile_state ._has_orm_entities = True
2367+ if is_current_entities :
2368+ if compile_state ._primary_entity is None :
2369+ compile_state ._primary_entity = self
2370+ compile_state ._has_mapper_entities = True
2371+ compile_state ._has_orm_entities = True
23522372
23532373 entity = entity ._annotations ["parententity" ]
23542374 entity ._post_inspect
0 commit comments