Skip to content

Incorrect column error does not exist on CASE querying #3022

Open
@pingu-codes

Description

@pingu-codes

Version

1.24.0

What happened?

When using querying CASE results using an sqlc parameter sqlc generate produces column 'x' does not exist errors, despite including those columns in the result struct.

Relevant log output

No response

Database schema

CREATE TABLE authors (
  id   BIGSERIAL PRIMARY KEY,
  name text      NOT NULL,
  bio  text,
  
created_at TIMESTAMP NOT NULL
);

CREATE TABLE books (
    id   BIGSERIAL PRIMARY KEY,
  name text      NOT NULL,
  author_id INT NOT NULL REFERENCES authors(id),
created_at TIMESTAMP NOT NULL
 )

SQL queries

-- name: GetExample :many
with author_times AS (SELECT author_id,
        COUNT(*) AS time_count,
        ROW_NUMBER() OVER(
            PARTITION BY author_id,
            time_of_day
            ORDER BY COUNT(*) DESC
        ) AS rn,
        time_of_day
    FROM
(SELECT  
b.author_id as author_id,
CASE
                    WHEN b.created_at::time >= TIME '07:00:00'
                    AND b.created_at::time < TIME '12:00:00' THEN 'breakfast'
                    WHEN b.created_at::time >= TIME '12:00:00'
                    AND b.created_at::time < TIME '17:00:00' THEN 'lunch'
                    WHEN b.created_at::time >= TIME '17:00:00'
                    AND b.created_at::time < TIME '22:00:00' THEN 'dinner'
                    ELSE 'night'
                END AS time_of_day
                FROM books b) AS subquery
                 GROUP BY author_id,
        time_of_day
                      )
                      SELECT * FROM author_times WHERE rn = 1 AND time_of_day = sqlc.arg(test);

Configuration

No response

Playground URL

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

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

    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