Skip to content

aliased() of an aliased CTE / aliased subquery entity drops the selectable; CTE.alias() of an alias renders undefined name (2.0.x / 2.1 fix) #13583

Description

@zzzeek

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingormsql

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions