Skip to content

CTE WITH RECURSIVE cannot find column in temp table#2187

@jlisthood

Description

@jlisthood

Version

1.17.2

What happened?

We tried to generate go code for a CTE using WITH RECURSIVE, but sqlc breaks.

Note: this may be a duplicate of #1912

Relevant log output

sqlc generate failed. # package db query.sql:41:7: column "parent" does not exist

Database schema

CREATETABLEcase_intent_version ( version_id SERIALNOT NULLPRIMARY KEY, reviewer TEXTNOT NULL, created_at TIMESTAMP WITH TIME ZONENOT NULL DEFAULT NOW() ); CREATETABLEcase_intent ( id SERIALNOT NULLPRIMARY KEY, case_intent_string TEXTNOT NULL, description TEXTNOT NULL, author TEXTNOT NULL ); CREATETABLEcase_intent_parent_join ( case_intent_id BIGINTNOT NULL, case_intent_parent_id BIGINTNOT NULL, constraint fk_case_intent_id foreign key (case_intent_id) references case_intent(id), constraint fk_case_intent_parent_id foreign key (case_intent_parent_id) references case_intent(id) ); CREATETABLEcase_intent_version_join ( case_intent_id BIGINTNOT NULL, case_intent_version_id INTNOT NULL, constraint fk_case_intent_id foreign key (case_intent_id) references case_intent(id), constraint fk_case_intent_version_id foreign key (case_intent_version_id) references case_intent_version(version_id) );

SQL queries

-- name: ListCaseIntentHistory :many WITH RECURSIVE descendants AS ( SELECT case_intent_parent_id AS parent, case_intent_id AS child, 1AS lvl FROM case_intent_parent_join UNION ALLSELECTd.parentas parent, p.case_intent_idas child, d.lvl+1as lvl FROM descendants d JOIN case_intent_parent_join p ONd.child=p.case_intent_parent_id ) select distinct child, 'child' group_ from descendants where parent = @case_intent_id unionselect distinct parent, 'parent' group_ from descendants where child = @case_intent_id ORDER BY child;

Configuration

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

Playground URL

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

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions