Skip to content

WITH RECURSIVE JOIN table alias does not exist #3924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
farwydi opened this issue Apr 10, 2025 · 0 comments
Open

WITH RECURSIVE JOIN table alias does not exist #3924

farwydi opened this issue Apr 10, 2025 · 0 comments

Comments

@farwydi
Copy link

farwydi commented Apr 10, 2025

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

@farwydi farwydi added the bug Something isn't working label Apr 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant