Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

ORM alias on CTE doesnt separate out things correctly #12364

Copy link
Copy link
@zzzeek

Description

@zzzeek
Issue body actions

Discussed in #11164

from sqlalchemy import literal
from sqlalchemy import select
from sqlalchemy import union_all
from sqlalchemy.orm import aliased
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column


class Base(DeclarativeBase):
    pass

class A(Base):
    __tablename__ = "a"
    i: Mapped[int] = mapped_column(primary_key=True)
    j: Mapped[int] = mapped_column()

class B(Base):
    __tablename__ = "b"
    i: Mapped[int] = mapped_column(primary_key=True)
    j: Mapped[int | None] = mapped_column()

# fails
a = aliased(A, select(A).where(A.i > A.j).cte("filtered_a"))
ac = a

# works
#a = select(A).where(A.i > A.j).cte("filtered_a")
#ac = a.c

a1 = select(ac.i, literal(1, literal_execute=True).label("j"))
b = select(B).join(a, ac.i == B.i).where(B.j.is_not(None))

query = union_all(a1, b)
print(query)
candiduslynx

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingSomething isn't workingnear-term releaseaddition to the milestone which indicates this should be in a near-term releaseaddition to the milestone which indicates this should be in a near-term releasesql

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.