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

Table aliases required to disambiguate duplicated column in a JOIN with SQLite #2282

Open
jamietanna opened this issue May 21, 2023 · 3 comments · May be fixed by #3293
Open

Table aliases required to disambiguate duplicated column in a JOIN with SQLite #2282

jamietanna opened this issue May 21, 2023 · 3 comments · May be fixed by #3293

Comments

@jamietanna
Copy link

jamietanna commented May 21, 2023

Version

1.17.2

What happened?

When trying to write a query that uses multiple tables with duplicated columns (through a NATURAL JOIN) it appears we need to use table aliases, instead of being able to reference the table name.

Relevant log output

# package db
queries.sql:59:3: column "engine" does not exist
exit status 1
internal/datasources/awsrds/db/generate.go:3: running "go": exit status 1

Database schema

CREATE TABLE IF NOT EXISTS aws_rds_databases (
  account_id TEXT NOT NULL,
  region TEXT NOT NULL,
  arn TEXT NOT NULL,
  name TEXT NOT NULL,
  engine TEXT NOT NULL,
  engine_version TEXT NOT NULL,

  -- tags is a JSON object
  tags TEXT NOT NULL,

  UNIQUE (account_id, region, arn) ON CONFLICT REPLACE
);

CREATE TABLE IF NOT EXISTS aws_rds_databases_engines (
  engine TEXT NOT NULL,
  engine_version TEXT NOT NULL,
  deprecation TEXT NOT NULL,

  UNIQUE (engine, engine_version) ON CONFLICT REPLACE
);

SQL queries

------------------
-- this is expected to fail, due to:
--> # package db
--> queries.sql:59:3: column reference "engine" is ambiguous

-- name: RetrieveAllWithDeprecation :many
select
  arn,
  name,
  engine
from
  aws_rds_databases
  natural join aws_rds_databases_engines;

------------------
-- expected to work
-- name: RetrieveAllWithDeprecation :many
select
  arn,
  name,
  aws_rds_databases.engine
from
  aws_rds_databases r
  natural join aws_rds_databases_engines
;

------------------
-- this works, however
-- name: RetrieveAllWithDeprecation :many
select
  arn,
  name,
  r.engine
from
  aws_rds_databases r
  natural join aws_rds_databases_engines
;

Configuration

version: 2
sql:
  - engine: "sqlite"
    schema: "schema.sql"
    queries: "queries.sql"
    gen:
      go:
        package: db
        out: .

Playground URL

https://play.sqlc.dev/p/9c6cd6f2df6585a2c1b5d5762b350179a9c78af984f8a657233c433153ca8732

What operating system are you using?

Linux, macOS

What database engines are you using?

SQLite

What type of code are you generating?

Go

@jamietanna jamietanna added bug Something isn't working triage New issues that hasn't been reviewed labels May 21, 2023
@kyleconroy kyleconroy added 🔧 golang 💻 darwin 📚 sqlite 💻 linux and removed triage New issues that hasn't been reviewed labels Jun 6, 2023
@mvdan
Copy link

mvdan commented Jun 13, 2023

Duplicate of #2271, I think.

@orisano
Copy link
Contributor

orisano commented Jul 11, 2023

This issue seems to be different from #2271.

case *ast.RangeVar:
fqn, err := ParseTableName(n)
if err != nil {
return nil, err
}
table, cerr := qc.GetTable(fqn)
if cerr != nil {
// TODO: Update error location
// cerr.Location = n.Location
// return nil, *cerr
return nil, cerr
}
if n.Alias != nil {
table.Rel = &ast.TableName{
Catalog: table.Rel.Catalog,
Schema: table.Rel.Schema,
Name: *n.Alias.Aliasname,
}
}
tables = append(tables, table)

This prevents searching by the original table name.

@kyleconroy
Copy link
Collaborator

We've upgraded from column reference error to a nil pointer dereference

https://play.sqlc.dev/p/333ef002c390049e4acb9d65e34b7823ff65a27b01ee94558630045ec534bee1

@nusr nusr linked a pull request Mar 26, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants