sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Inferring columns from some CTE's is invalid

Open blanchardglen opened this issue 3 weeks ago • 1 comments

Version

1.28.0

What happened?

I have the following CTE in my queries.sql

WITH w_pending_sale_status as (
    select * from 
    (values ('SAVED'), ('IDLE'), ('IN PROGRESS'), ('HELD')) 
    as pending_sale_status(status)
)
SELECT status FROM w_pending_sale_status;

Once generated the go file's sql is this

WITH w_pending_sale_status as (
select  from 
    (values ('SAVED'), ('IDLE'), ('IN PROGRESS'), ('HELD')) 
    as pending_sale_status(status)
)
SELECT status FROM w_pending_sale_status;

Notice it is missing the * so that the error when execting from postgres is column status is not a valid column name

Relevant log output

NA

Database schema


SQL queries

-- name: SaleStatus :many
WITH w_pending_sale_status as (
    select * from 
    (values ('SAVED'), ('IDLE'), ('IN PROGRESS'), ('HELD')) 
    as pending_sale_status(status)
)
SELECT status FROM w_pending_sale_status;

Configuration

version: "2"
sql:
  - engine: "postgresql"
    queries: "db/queries"
    schema: "db/migrations"
    codegen:
    - plugin: golang
      out: "gen/db"
      options:
        package: "gendb"
        sql_package: "pgx/v5"
        overrides:
        - column: "transactions.amount"
          go_type:
            import: "github.com/shopspring/decimal"
            type: "Decimal"
            pointer: true
        - db_type: "pg_catalog.numeric"
          go_type: "github.com/shopspring/decimal.Decimal"
        - db_type: "pg_catalog.numeric"
          go_type: "github.com/shopspring/decimal.NullDecimal"
          nullable: true

Playground URL

No response

What operating system are you using?

Windows

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

blanchardglen avatar Nov 28 '25 07:11 blanchardglen

A workaround for now is to use this CTE instead

WITH w_pending_sale_status as (
select unnest(ARRAY['SAVED', 'IDLE', 'IN PROGRESS', 'HELD'])::text as status
)
SELECT status FROM w_pending_sale_status;

blanchardglen avatar Dec 01 '25 00:12 blanchardglen