- Notifications
You must be signed in to change notification settings - Fork 978
Closed
Labels
Description
Version
1.20.0
What happened?
I'm getting a relation "recursive_table_name" does not exist error when running sqlc generate on a query that has a WITH RECURSIVE subquery.
For instance this query (simplified as a test case) throws a relation "search_tree" does not exist error
-- name: GetLatestVersionWithSubquery :oneSELECT*FROM versions WHEREversions.idIN ( WITH RECURSIVE search_tree(id, chain_id, chain_counter) AS ( SELECTbase.id, base.idAS chain_id, 0as chain_counter FROM versions AS base WHEREversions.previous_version_id IS NULLUNION ALLSELECTv.id, search_tree.chain_id, v.parent_lead_time_estimate_id, v.chain_counter+1FROM versions AS v INNER JOIN search_tree ONsearch_tree.id=v.previous_version_id ) SELECT DISTINCTON (search_tree.chain_id) search_tree.idFROM search_tree ORDER BYsearch_tree.chain_id, chain_counter DESC );But that same recursive CTE query parses fine on its own when it's not in a subquery:
-- name: GetLatestVersion :one WITH RECURSIVE search_tree(id, chain_id, chain_counter) AS ( SELECTbase.id, base.idAS chain_id, 0as chain_counter FROM versions AS base WHEREversions.previous_version_id IS NULLUNION ALLSELECTv.id, search_tree.chain_id, v.parent_lead_time_estimate_id, v.chain_counter+1FROM versions AS v INNER JOIN search_tree ONsearch_tree.id=v.previous_version_id ) SELECT DISTINCTON (search_tree.chain_id) search_tree.idFROM search_tree ORDER BYsearch_tree.chain_id, chain_counter DESC;It seems like something in the SQL parser isn't matching the CTE's name with the auxiliary statement, only when it's nested in a subquery. But the real version of this query that I'm trying to write does run fine against real postgres, so it seems that the syntax is valid.
Relevant log output
query.sql:24:1: relation "search_tree" does not existDatabase schema
CREATETABLEversions ( id BIGSERIALPRIMARY KEY, name TEXT, previous_version_id bigintNOT NULL );SQL queries
WITH RECURSIVE search_tree(id, chain_id, chain_counter) AS ( SELECTbase.id, base.idAS chain_id, 0as chain_counter FROM versions AS base WHEREversions.previous_version_id IS NULLUNION ALLSELECTv.id, search_tree.chain_id, v.parent_lead_time_estimate_id, v.chain_counter+1FROM versions AS v INNER JOIN search_tree ONsearch_tree.id=v.previous_version_id ) SELECT DISTINCTON (search_tree.chain_id) search_tree.idFROM search_tree ORDER BYsearch_tree.chain_id, chain_counter DESC;Configuration
{"version": "1","packages": [{"path": "db","engine": "postgresql","schema": "query.sql","queries": "query.sql"}]}Playground URL
https://play.sqlc.dev/p/fd638d4cd5e89589d00806d8575daa55fd45f28199ffa1365a43d879dd9a5915
What operating system are you using?
macOS
What database engines are you using?
PostgreSQL
What type of code are you generating?
Go