Skip to content

sqlc hallucinates ambiguous column references when no ambiguity exists  #2538

Closed

Description

@andrewmbenton

Version

1.19.1

What happened?

Discovered while working on #2537, sqlc returns an ambiguous column reference error for some queries which run fine with psql. See examples below.

To fix we'll need to update findColumnForRef() in output_columns.go I believe.

Relevant log output

query.sql:9:1: column reference "name" is ambiguous: if you want to skip this validation, set 'strict_order_by' to false

Database schema

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

SQL queries

SELECT a.name
FROM authors a JOIN authors b ON a.id = b.id
ORDER BY name;

SELECT a.name AS name
FROM authors a JOIN authors b ON a.id = b.id
ORDER BY name;

Configuration

{
  "version": "1",
  "packages": [
    {
      "path": "db",
      "engine": "postgresql",
      "schema": "query.sql",
      "queries": "query.sql"
    }
  ]
}

Playground URL

https://play.sqlc.dev/p/8e9c3fa8f50201433c489b118a731130a7011574ec53d27bbe460eb1978f2f13

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

Activity

  1. added
    bugSomething isn't working
    triageNew issues that hasn't been reviewed
    on Jul 27, 2023
  2. changed the title [-]sqlc hallucinates ambiguous columns when no ambiguity exists [/-] [+]sqlc hallucinates ambiguous column references when no ambiguity exists [/+] on Jul 27, 2023
  3. andrewmbenton commented on Jul 28, 2023

    @andrewmbenton
    MemberAuthor

    The urgency on resolving this isn't super high. The obvious workaround is to just fully qualify the column names in your order by clauses. In both queries above, using ORDER BY a.name resolves the sqlc confusion about ambiguity.

  4. andrewmbenton commented on Oct 2, 2023

    @andrewmbenton
    MemberAuthor
  5. kyleconroy commented on Oct 24, 2023

    @kyleconroy
    Collaborator

    This is fixed in v1.23.0 by enabling the database-backed query analyzer. We added a test case for this issue so it won’t break in the future.

    You can play around with the working example on the playground

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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