sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
11.83k stars 762 forks source link

CTE inside subquery not working #3156

Open ross96D opened 6 months ago

ross96D commented 6 months ago

Version

1.25.0

What happened?

It seems that the parser cannot identify the relation created from the cte when the cte is inside a subquery

Relevant log output

query.sql:1:1: relation "cte_authors" does not exist

Database schema

CREATE TABLE authors (
  id   BIGSERIAL PRIMARY KEY,
  name text      NOT NULL,
  bio  text
);

SQL queries

-- name: test :many
SELECT * FROM (
  WITH cte_authors AS (
    SELECT * FROM authors
  )
  SELECT * FROM authors JOIN cte_authors ON autors.id = cte_autors.et_id
) AS sub WHERE true;

Configuration

{
  "version": "2",
  "sql": [{
    "schema": "schema.sql",
    "queries": "query.sql",
    "engine": "postgresql",
    "gen": {
      "go": {
        "out": "db"
      }
    }
  }]
}

Playground URL

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

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

matteobassan commented 3 weeks ago

Same problem here. Using CTE is quite unusable with this package, given the other issues related (#3128)