Skip to content

Resolving reference to CTE column fails #3219

Closed
simonklee/sqlc
#1

Description

@simonklee

Version

1.25.0

What happened?

Resolving references to multiple CTE's with arguments fails.

Relevant log output

sqlc generate failed.
# package 
query.sql:5:28: table alias "t1_ids" does not exist

Database schema

CREATE TABLE t1
(
    id SERIAL NOT NULL PRIMARY KEY
);

SQL queries

-- name: CTERef :one
WITH t1_ids AS (
    SELECT id FROM t1
)
SELECT * FROM t1_ids WHERE t1_ids.id = sqlc.arg('id');

Configuration

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

Playground URL

https://play.sqlc.dev/p/9048b486f822c300301650867612e42bef8f8d512dfd12d84ad3a4cd210f4ef7

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

Activity

  1. added
    bugSomething isn't working
    triageNew issues that hasn't been reviewed
    on Feb 24, 2024
  2. added 3 commits that reference this issue on Feb 25, 2024
    17133c4
    e477f77
    87aa1a9
  3. mbesida commented on Mar 17, 2024

    @mbesida

    Does this also relate to following problem? I'm trying to do conditional insert

    WITH 
        data (id, foo, bar) AS ( VALUES ($1, $2, $3) ), 
        insert_data AS (
            SELECT 
              d.id, 
              u.id as userId, 
              d.bar
            FROM data d JOIN users u ON d.foo = u.someField
        )
    INSERT INTO myTable(id, userId, bar)
    SELECT * FROM insert_data
    RETURNING *;
    

    sqlc generate gives error: column "id" does not exist pointing to line d.id in insert_data's select

  4. simonklee commented on Mar 24, 2024

    @simonklee
    ContributorAuthor

    I've been using a patched version of sqlc with my PR applied for a month and I haven't experienced any issues with it.

  5. simonklee commented on Mar 24, 2024

    @simonklee
    ContributorAuthor

    Does this also relate to following problem? I'm trying to do conditional insert

    I think this is the same issue. Should be fairly easy to add the use-case as a regression to the test suite.

  6. simonklee commented on Mar 28, 2024

    @simonklee
    ContributorAuthor

    This should be re-opened given: 3f129e7

  7. added a commit that references this issue on Apr 3, 2024
    65aa1dd
  8. added a commit that references this issue on Apr 4, 2024
    3a3e387
  9. added a commit that references this issue on Apr 4, 2024
    6e3b65c
  10. added 2 commits that reference this issue on Apr 5, 2024
    1898589
    403080b
  11. added 2 commits that reference this issue on Jun 21, 2024
    ccd776d
    e519f4a
  12. added 2 commits that reference this issue on Oct 13, 2025
    1cc9725
    b38f69c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriageNew issues that hasn't been reviewed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions