8000 Fix CTE column key resolution for unlabeled expressions in RETURNING clause by IamGroooooot · Pull Request #12636 · sqlalchemy/sqlalchemy · GitHub
[go: up one dir, main page]

Skip to content

Fix CTE column key resolution for unlabeled expressions in RETURNING clause #12636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

IamGroooooot
Copy link
@IamGroooooot IamGroooooot commented May 30, 2025

Fix CTE column key resolution for unlabeled expressions in RETURNING clause

Description

This PR fixes an issue #12271 where unlabeled expressions in RETURNING clauses lose their column keys when used in CTEs.

Fixes: #12271

Problem

When creating a CTE from DELETE statements with RETURNING clauses containing unlabeled expressions the _make_proxy method got AssertionError because key was being None.

# This fails - because in unlabeled expression key was being None
stmt = delete(t).returning(
    t.c.id,
    t.c.id * -1  # Unlabeled expression
).cte()
assert stmt.c.id.name == "id"  # AssertionError: key was None

Solution

Modified _make_proxy to ensure key is never None by falling back to self._anon_key_label

To validate this fix, the following two test cases have been added:

  • test_returning_cte_labeled_expression - Ensures labeled expressions continue to work as expected
  • test_returning_cte_multiple_unlabeled_expressions - Verifies that multiple unlabeled expressions are handled correctly

Checklist

This pull request is:

  • A documentation / typographical / small typing error fix
    • Good to go, no issue or tests are needed
  • A short code fix
    • please include the issue number, and create an issue if none exists, which
      must include a complete example of the issue. one line code fixes without an
      issue and demonstration will not be accepted.
    • Please include: Fixes: #<issue number> in the commit message
    • please include tests. one line code fixes without tests will not be accepted.
  • A new feature implementation
    • please include the issue number, and create an issue if none exists, which must
      include a complete example of how the feature would look.
    • Please include: Fixes: #<issue number> in the commit message
    • please include tests.

Have a nice day!

Copy link
Member
@CaselIT CaselIT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening this

).cte()

assert stmt.c.id is not None
assert all(col is not None for col in stmt.c)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could also assert that the len of stmt.c is the expected one (5)

Copy link
Author
@IamGroooooot IamGroooooot May 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍. added the len(stmt.c) check!

@CaselIT CaselIT requested a review from zzzeek May 30, 2025 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

look into using more robust columns_plus_names for DML, why didnt we use this
2 participants
0