You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
CREATETABLEauthors (
id BIGSERIALPRIMARY KEY,
name textNOT NULL,
bio text
);
SQL queries
-- name: test :manySELECT*FROM (
WITH cte_authors AS (
SELECT*FROM authors
)
SELECT*FROM authors JOIN cte_authors ONautors.id=cte_autors.et_id
) AS sub WHERE true;
This has a silent form as well, and I think it is the more serious one.
With database: managed: true, or analyzer.database: true and a URI, the relation "x" does not exist error from sqlc's own analysis is discarded and the database's description of the statement is used in its place. Every column then takes its table's NOT NULL, so a column from the nullable side of an outer join is generated as never null.
CREATETABLEauthors (id BIGSERIALPRIMARY KEY, name textNOT NULL);
CREATETABLEbooks (id BIGSERIALPRIMARY KEY, author_id bigintNOT NULL, title textNOT NULL);
-- name: ListAuthors :manySELECTa.name, b.titleFROM authors a
LEFT JOIN books b ONb.author_id=a.idJOIN (WITH x AS (SELECT id FROM authors) SELECT id FROM x) t ONt.id=a.id;
On 1.31.1 and on main (3ad8b20), generating Go with sql_package: pgx/v5:
With no database: relation "x" does not exist, as reported here.
With a database: it generates, and Title is string. Move the WITH to the top of the statement and it is pgtype.Text, which is right.
#2644 and #2004 were closed as fixed by the database-backed analyzer, which is the path that gives the wrong type.
The cause is that the query catalog only registers the statement's own WITH. I have a fix on a fork that gives each nested statement a scope of its own, with end-to-end cases: Bockit@f7cedce. With it the query in #3206 generates too. I can open a pull request if a fix to this path is wanted.
The analysis was done with AI help and checked by me.
Activity
matteobassan commentedon Jul 9, 2024
Same problem here.
Using CTE is quite unusable with this package, given the other issues related (#3128)
Bockit commentedon Nov 14, 2026
This has a silent form as well, and I think it is the more serious one.
With
database: managed: true, oranalyzer.database: trueand a URI, therelation "x" does not existerror from sqlc's own analysis is discarded and the database's description of the statement is used in its place. Every column then takes its table'sNOT NULL, so a column from the nullable side of an outer join is generated as never null.On 1.31.1 and on main (3ad8b20), generating Go with
sql_package: pgx/v5:relation "x" does not exist, as reported here.Titleisstring. Move theWITHto the top of the statement and it ispgtype.Text, which is right.*in the statement as well:star expansion failed for query, as in Star expansion failed for query #2915.#2644 and #2004 were closed as fixed by the database-backed analyzer, which is the path that gives the wrong type.
The cause is that the query catalog only registers the statement's own
WITH. I have a fix on a fork that gives each nested statement a scope of its own, with end-to-end cases: Bockit@f7cedce. With it the query in #3206 generates too. I can open a pull request if a fix to this path is wanted.The analysis was done with AI help and checked by me.