sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

LATERAL subquery cannot resolve outer table columns

Open WillAbides opened this issue 1 month ago • 0 comments

Version

1.30.0

What happened?

sqlc generate errored with the message schema.sql:14:16: column "val" does not exist on a schema that is valid SQL

This happens when the schema has a view with a LATERAL join that selects a column from the outer query.

I've found a workaround where it works when I wrap it in a CASE WHEN true.

AI disclaimer: This is a real issue I encountered with a real schema, but I used AI to reduce this to a minimal reproducible schema.

Relevant log output

$ sqlc generate
# package queries
schema.sql:12:7: column "val" does not exist

Database schema

CREATE TABLE foo (id INTEGER PRIMARY KEY, val INTEGER)
;

CREATE VIEW foo_lateral AS
SELECT
  t.val,
  sub.result
FROM
  foo t
  CROSS JOIN LATERAL (
    SELECT
      t.val AS result -- ERROR: column "val" does not exist
      --      CASE WHEN true THEN t.val END AS result  -- This works!
    FROM
      foo
    LIMIT
      1
  ) sub
;

SQL queries

-- no queries required for this

Configuration

version: "2"
sql:
  - engine: postgresql
    schema: schema.sql
    gen:
      go: {out: queries}

Playground URL

No response

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

WillAbides avatar Nov 13 '25 18:11 WillAbides