Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlc hallucinates ambiguous column references when no ambiguity exists #2538

Closed
andrewmbenton opened this issue Jul 27, 2023 · 4 comments
Closed

Comments

@andrewmbenton
Copy link
Collaborator

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

@andrewmbenton andrewmbenton added bug Something isn't working triage New issues that hasn't been reviewed labels Jul 27, 2023
@andrewmbenton andrewmbenton changed the title sqlc hallucinates ambiguous columns when no ambiguity exists sqlc hallucinates ambiguous column references when no ambiguity exists Jul 27, 2023
@andrewmbenton
Copy link
Collaborator Author

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.

@kyleconroy
Copy link
Collaborator

@andrewmbenton
Copy link
Collaborator Author

Still broken if you use the postgresql or mysql engines: https://play.sqlc.dev/p/3d1c2485fb8a777557b4de50bb3f7d60e17ec1a8fdcbdb42d674a5b5eae967ad

@andrewmbenton andrewmbenton reopened this Oct 2, 2023
@andrewmbenton andrewmbenton added 📚 mysql 📚 postgresql analyzer 🔧 golang and removed bug Something isn't working triage New issues that hasn't been reviewed 🔧 golang labels Oct 2, 2023
kyleconroy added a commit that referenced this issue Oct 18, 2023
kyleconroy added a commit that referenced this issue Oct 18, 2023
* test: Add case for #2132
* test: Add case for #2152
* test: Mark case for #2152
* test: Add case for #2187
* test: Add case for #2226
* test: Add case for #2364
* test: Add case for #2386
* test: Add case for #2538
* test: Add case for #2644
* test: Add case for #2731
@kyleconroy
Copy link
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
Projects
None yet
Development

No branches or pull requests

2 participants