Skip to content

LATERAL subquery cannot resolve outer table columnsΒ #4182

@WillAbides

Description

@WillAbides

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions