Sub-issue of #13581, covering the part of that issue that can be fixed in 2.0.x and 2.1 without changing behavior for existing correct usage. See #13581 for the full report, reproduction and analysis.
Scope
ORM: aliased() with no explicit selectable, called against an existing aliased() entity, aliases that entity's own selectable instead of the mapped table when the selectable is anything other than plain aliases of the mapped table:
- a CTE, or an alias of a CTE
- an alias of a subquery, i.e.
aliased() applied a second time
- a lateral subquery
- a table sample
In addition:
name= is applied when the existing selectable is a subquery (currently ignored)
- the
adapt_on_names setting of the existing entity is carried along
Entities that are only aliases of the mapped table, including flat aliases of joined inheritance, keep their current code path, so rendered SQL and anonymous alias names are unchanged.
Core:
cte.alias("a1").alias("a2") renders c AS a2 against the original CTE, rather than a1 AS a2, which refers to a name not present in the WITH clause
Join._anonymous_fromclause(name=..., flat=True) against a join of anonymously named aliases no longer embeds the anonymous label within a literal name, which failed with KeyError at execution
Related
aliased() against a with_polymorphic() entity, which currently discards
the polymorphic selectable, is fixed in the same change under #13584.
Sub-issue of #13581, covering the part of that issue that can be fixed in 2.0.x and 2.1 without changing behavior for existing correct usage. See #13581 for the full report, reproduction and analysis.
Scope
ORM:
aliased()with no explicit selectable, called against an existingaliased()entity, aliases that entity's own selectable instead of the mapped table when the selectable is anything other than plain aliases of the mapped table:aliased()applied a second timeIn addition:
name=is applied when the existing selectable is a subquery (currently ignored)adapt_on_namessetting of the existing entity is carried alongEntities that are only aliases of the mapped table, including flat aliases of joined inheritance, keep their current code path, so rendered SQL and anonymous alias names are unchanged.
Core:
cte.alias("a1").alias("a2")rendersc AS a2against the original CTE, rather thana1 AS a2, which refers to a name not present in the WITH clauseJoin._anonymous_fromclause(name=..., flat=True)against a join of anonymously named aliases no longer embeds the anonymous label within a literal name, which failed withKeyErrorat executionRelated
aliased()against awith_polymorphic()entity, which currently discardsthe polymorphic selectable, is fixed in the same change under #13584.