Skip to content

Conversation

@mafredri
Copy link

@mafredri mafredri commented Oct 29, 2025

EDIT: Sorry, I did not intend to open this PR over here yet, it was meant for coder/sqlc. But since it's here already. I'll keep it open.


While adding a simple EXISTS condition, I ran into an sqlc bug because it wasn't tracking scope for CTEs.

query.sql:11:20: column reference "owner_id" is ambiguous
query.sql:24:20: column "w_owner_id" does not exist
query.sql:38:20: column reference "owner_id" is ambiguous

The query could look something like this:

WITH cte AS (
	SELECT owner_id
	FROM table_with_owner_id
	WHERE EXISTS (SELECT 1 FROM another_table_with_owner_id)
)
SELECT * FROM cte;

Test queries have been added that reproduce the bug.

Some of the existing testdata also had to be updated because the tracking got improved.


This bug was initially solved via Coder Tasks (Claude Code), but the solution was messy and followed the "throw code at it until it works", as such it has been mostly rewritten.

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. 🔧 golang labels Oct 29, 2025
mafredri added a commit to coder/coder that referenced this pull request Oct 29, 2025
mafredri added a commit to coder/coder that referenced this pull request Oct 29, 2025
@mafredri mafredri force-pushed the mafredri/fix-ambiguous-column branch 5 times, most recently from 5f67321 to 7c1a82b Compare October 29, 2025 12:09
mafredri added a commit to coder/coder that referenced this pull request Oct 29, 2025
mafredri added a commit to coder/coder that referenced this pull request Oct 29, 2025
mafredri added a commit to coder/coder that referenced this pull request Oct 29, 2025
mafredri added a commit to coder/coder that referenced this pull request Oct 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files. 🔧 golang

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant