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 alias does not exist for subquery #3639

Open
waterfountain1996 opened this issue Oct 7, 2024 · 1 comment
Open

Table alias does not exist for subquery #3639

waterfountain1996 opened this issue Oct 7, 2024 · 1 comment
Labels
📚 sqlite bug Something isn't working

Comments

@waterfountain1996
Copy link

Version

1.27.0

What happened?

SQLC fails to generate a query where I try to use a column from a subquery in a WHERE clause to compare against a query argument, which is a valid SQL syntax for SQLite. The same query compiles successfully I use a literal value instead of an argument.

P.S. I've tried reproducing it in the playground and it seems that it actually compiles successfully on v1.25.0 but not on v1.26 or newer.

Relevant log output

# package db
queries.sql:7:7: table alias "a" does not exist

Database schema

CREATE TABLE posts (
  id   BIGSERIAL PRIMARY KEY,
  tags TEXT -- JSON array of hashtags like ["#foo", "#bar"]
);

SQL queries

-- name: GetPostIDsByTag :many
SELECT DISTINCT a.id
FROM (
	SELECT p.id, ltrim(json_each.value ->> '$.name', '#') AS tag
	FROM posts p, json_each(p.tags)
) a
WHERE a.tag = 'foo';

Configuration

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

Playground URL

https://play.sqlc.dev/p/33831f5ac4e36ae1654164ae929c04ed981a51b135d20aca35e492573f00c8b0

What operating system are you using?

macOS

What database engines are you using?

SQLite

What type of code are you generating?

Go

@waterfountain1996 waterfountain1996 added the bug Something isn't working label Oct 7, 2024
@dosubot dosubot bot added the 📚 sqlite label Oct 7, 2024
@waterfountain1996
Copy link
Author

A simplified version of the query above

SELECT DISTINCT p.id
FROM posts p, json_each(p.tags) j
WHERE ltrim(j.value ->> '$.name', '#') = ?

gives the same table alias "j" does not exist error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📚 sqlite bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant