Skip to content
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

Error when using CTE and self join #2132

Closed
matbur opened this issue Mar 7, 2023 · 2 comments
Closed

Error when using CTE and self join #2132

matbur opened this issue Mar 7, 2023 · 2 comments

Comments

@matbur
Copy link

matbur commented Mar 7, 2023

Version

1.17.2

What happened?

When using CTE (WITH clause) in query and self join, sqlc doesn't see any columns. When using join from another table/view it works as expected.

Relevant log output

sqlc generate failed.

# package db
query.sql:6:5: column "id" does not exist

Database schema

No response

SQL queries

-- name: Q :many
WITH
    items1 AS (SELECT 'id'::TEXT AS id, 'name'::TEXT AS name),
    items2 AS (SELECT 'id'::TEXT AS id, 'name'::TEXT AS name)
SELECT
    i1.id AS id1,
    i2.id AS id2
FROM
    items1 i1
        JOIN items1 i2 ON 1 = 1;
        -- change ^ above items1 to items2 to fix

Configuration

{
  "version": "1",
  "packages": [
    {
      "path": "db",
      "engine": "postgresql",
      "schema": "query.sql",
      "queries": "query.sql"
    }
  ]
}

Playground URL

https://play.sqlc.dev/p/af3970dbc2ab065393d42b65a2bedf37b0d73734214ca9a42579829dfb8e6de7

What operating system are you using?

No response

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

@matbur matbur added bug Something isn't working triage New issues that hasn't been reviewed labels Mar 7, 2023
@matbur matbur changed the title Error when using WITH and self join Error when using CTE and self join Mar 7, 2023
@kyleconroy kyleconroy added 📚 postgresql 🔧 golang and removed triage New issues that hasn't been reviewed labels Jun 7, 2023
kyleconroy added a commit that referenced this issue Oct 18, 2023
kyleconroy added a commit that referenced this issue Oct 18, 2023
* test: Add case for #2132
* test: Add case for #2152
* test: Mark case for #2152
* test: Add case for #2187
* test: Add case for #2226
* test: Add case for #2364
* test: Add case for #2386
* test: Add case for #2538
* test: Add case for #2644
* test: Add case for #2731
@kyleconroy
Copy link
Collaborator

This is fixed in v1.23.0 by enabling the database-backed query analyzer. We added a test case for this issue so it won’t break in the future.

You can play around with the working example on the playground

@matbur
Copy link
Author

matbur commented Oct 25, 2023

Thank you for fixing this! Now it works perfectly.

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

No branches or pull requests

2 participants