Skip to content

incorrect ambiguous reference check #2398

Closed

Description

@Stumble

Version

1.18.0

What happened?

There seems to be a minor bug in checking ambiguous references when joning tables while using alias.

To reproduce, change the sqlc/internal/endtoend/testdata/order_by_non_existing_column/postgresql to

-- Example queries for sqlc
CREATE TABLE authors (
  id   INT
);

CREATE TABLE books (
  id   INT,
  author_id INT,
  price INT
);

-- name: ListAuthors :many
SELECT id FROM authors
ORDER BY adfadsf;

-- name: ListAuthorsByCheapestBook :many
SELECT
author_id, min(b.price) as min_price
From books b inner join authors a on a.id = b.author_id
GROUP BY b.author_id
ORDER BY min_price;

The column of min_price is not ambiguous but sqlc reports:

# package 
query.sql:13:1: column reference "adfadsf" not found: if you want to skip this validation, set 'strict_order_by' to false
query.sql:17:1: column reference "min_price" is ambiguous: if you want to skip this validation, set 'strict_order_by' to false

Relevant log output

query.sql:13:1: column reference "adfadsf" not found: if you want to skip this validation, set 'strict_order_by' to false
query.sql:17:1: column reference "min_price" is ambiguous: if you want to skip this validation, set 'strict_order_by' to false

Database schema

CREATE TABLE authors (
  id   INT
);

CREATE TABLE books (
  id   INT,
  author_id INT,
  price INT
);

SQL queries

-- name: ListAuthorsByCheapestBook :many
SELECT
author_id, min(b.price) as min_price
From books b inner join authors a on a.id = b.author_id
GROUP BY b.author_id
ORDER BY min_price;

Configuration

No response

Playground URL

No response

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 Jun 30, 2023
  2. akutschera commented on Jul 1, 2023

    @akutschera
    Contributor

    That's an interesting one. I believe this has been in the code for a while.
    What happens seems to be this:
    For each column name in a GROUP BY or ORDER BY clause, we check, if we have it in a table definition.
    When we cannot find it, we check the select statement. When we find it there (e.g. in an alias) we say we found it.
    Because we iterate over the number of tables in the statement, we will find it twice. That causes this problem.

    The PR above should fix this (and also #1886 which seems to be very similar to this one here).

  3. added a commit that references this issue on Jul 27, 2023
    5bd0257
  4. added a commit that references this issue on Jul 28, 2023
    7d6377f
  5. Stumble commented on Aug 29, 2023

    @Stumble
    Author

    Sorry for late reply, thanks for the fix!

  6. added a commit that references this issue on Oct 13, 2025
    219e197
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