Skip to content

WITH RECURSIVE JOIN table alias does not exist #3924

Open
@farwydi

Description

@farwydi

Version

1.28.0

What happened?

Hello I got

Relevant log output

sqlc generate failed.
# package 
query.sql:16:11: table alias "rc" does not exist

Database schema

CREATE TABLE user_referrals
(
    user_id     bigint                                 NOT NULL
        CONSTRAINT user_referrals_pk
            PRIMARY KEY
        CONSTRAINT user_referrals_users_id_fk
            REFERENCES users
            ON DELETE CASCADE,
    referrer_id bigint                                 NOT NULL
        CONSTRAINT user_referrals_users_id_fk_2
            REFERENCES users
            ON DELETE SET NULL,
    balance     numeric(38, 9)           DEFAULT 0.00  NOT NULL,
    created_at  timestamp with time zone DEFAULT NOW() NOT NULL,
    updated_at  timestamp with time zone DEFAULT NOW() NOT NULL
);

SQL queries

-- name: GetReferralChain :many
WITH RECURSIVE referral_chain AS (
    SELECT user_id,
           referrer_id,
           1 AS level
    FROM user_referrals r
    WHERE r.user_id = $1

    UNION ALL

    SELECT r.user_id,
           r.referrer_id,
           rc.level + 1
    FROM user_referrals r
             JOIN referral_chain rc ON r.user_id = rc.referrer_id
    WHERE rc.level < @level
)
SELECT *
FROM referral_chain
;

Configuration

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

Playground URL

https://play.sqlc.dev/p/2678bbd6fdc4d323f3464abd3f5e27175b26b74ea7ca0cc779ec7c2395f689ad

What operating system are you using?

Windows

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