sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Ambiguous column checking creates false positives

Open ncabatoff opened this issue 1 year ago • 0 comments

Version

1.25.0

What happened?

Here's an example: https://play.sqlc.dev/p/f32a811caf892026c11495a0059cc4eb041ba647e746f47316873f7bc85bd3bd

The CreateTitle query INSERT INTO titles (id, name, author_id) VALUES ($1, $2, (SELECT author_id FROM authors WHERE name = $3)); is flagged:

query.sql:22:95: column reference "name" is ambiguous

I don't believe it's ambiguous in the context of the sub-select.

Note that I see this problem with CTEs as well, I can provide an example if needed.

Relevant log output

query.sql:22:95: column reference "name" is ambiguous

Database schema

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

CREATE TABLE titles (
  id   BIGSERIAL PRIMARY KEY,
  name text      NOT NULL,
  author_id BIGINT NOT NULL ,
  CONSTRAINT fk_authors FOREIGN KEY(author_id) REFERENCES authors(id)
);

SQL queries

INSERT INTO titles (id, name, author_id) VALUES ($1, $2, (SELECT author_id FROM authors WHERE name = $3));

Configuration

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

Playground URL

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

What operating system are you using?

Linux, macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

ncabatoff avatar Apr 05 '24 19:04 ncabatoff