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

"Column does not exist" when selecting a subquery column #2989

Open
taleeus opened this issue Nov 17, 2023 · 1 comment
Open

"Column does not exist" when selecting a subquery column #2989

taleeus opened this issue Nov 17, 2023 · 1 comment
Labels
bug Something isn't working triage New issues that hasn't been reviewed
Milestone

Comments

@taleeus
Copy link

taleeus commented Nov 17, 2023

Version

1.23.0

What happened?

The column(s) in the SELECT statement is present in the subquery from which I'm selecting, but sqlc tells me otherwhise.

PS: I had to add table names to the columns in the subquery to make other sqlc errors about ambiguity disappear, but the query works fine in MySQL even without them.

Relevant log output

sqlc generate -f platform/db/config/sqlc.yaml
# package dbout
queries/device-associations.sql:3:2: column "serialnumber" does not exist

Database schema

-- Foreign keys omitted

CREATE TABLE `DeviceAssociation` (
  `serialNumber` varchar(191) NOT NULL,
  `createdAt` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `currentSerialNumber` varchar(191) DEFAULT NULL,
  `previousSerialNumber` varchar(191) DEFAULT NULL,
  PRIMARY KEY (`serialNumber`,`createdAt`),
  KEY `DeviceAssociation_previousSerialNumber_idx` (`previousSerialNumber`),
  KEY `DeviceAssociation_currentSerialNumber_idx` (`currentSerialNumber`),
  KEY `DeviceAssociation_createdAt_idx` (`createdAt` DESC)
);

SQL queries

-- name: DeviceAssociations :many
SELECT DISTINCT
	A.serialNumber,
	A.currentSerialNumber
FROM (
		SELECT
			DeviceAssociation.serialNumber AS serialNumber,
			DeviceAssociation.currentSerialNumber AS currentSerialNumber,
			MAX(DeviceAssociation.createdAt) AS createdAt
		FROM
			DeviceAssociation
		WHERE
			DeviceAssociation.serialNumber = sqlc.arg(serialNumber)
			AND DeviceAssociation.currentSerialNumber IS NOT NULL
		GROUP BY
			serialNumber,
			currentSerialNumber
	) AS A
LEFT JOIN (
		SELECT
			DeviceAssociation.serialNumber AS serialNumber,
			DeviceAssociation.previousSerialNumber AS previousSerialNumber,
			MAX(DeviceAssociation.createdAt) AS createdAt
		FROM
			DeviceAssociation
		WHERE
			DeviceAssociation.serialNumber = sqlc.arg(serialNumber)
			AND DeviceAssociation.previousSerialNumber IS NOT NULL
		GROUP BY
			serialNumber,
			previousSerialNumber
	) AS D ON
		A.serialNumber = D.serialNumber AND
		A.currentSerialNumber = D.previousSerialNumber
WHERE A.createdAt > D.createdAt;

Configuration

version: "2"
sql:
  - engine: "mysql"
    queries: "queries"
    schema: "schema.sql"
    gen:
      go:
        package: "dbout"
        out: "../../../app/dbout"
        emit_empty_slices: true
    database:
      uri: "${DATABASE_URL}"
    rules:
      - sqlc/db-prepare

Playground URL

No response

What operating system are you using?

macOS

What database engines are you using?

MySQL

What type of code are you generating?

Go

@taleeus taleeus added bug Something isn't working triage New issues that hasn't been reviewed labels Nov 17, 2023
@Jille
Copy link
Contributor

Jille commented Feb 26, 2024

Simplified test case: https://play.sqlc.dev/p/cefb6385989c2516f3d658e88e9f236f68973bcca0d63c60c4aa4d32a9221b46

Jille added a commit to Jille/sqlc that referenced this issue Feb 27, 2024
kyleconroy pushed a commit that referenced this issue Mar 28, 2024
* fix(resolve): fix resolving reference to CTEs

Fixed resolving refs to CTEs by adding CTEs to the aliasMap and indexing
its columns when resolving catalog references.

Fix #3219

* feat(compiler): Support subqueries in the FROM clause

issue #2989, #2400 and probably others

---------

Co-authored-by: Simon Klee <hello@simonklee.dk>
Jille added a commit to Jille/sqlc that referenced this issue Apr 3, 2024
kyleconroy pushed a commit that referenced this issue Apr 4, 2024
…#3310)

* fix(resolve): fix resolving reference to CTEs

Fixed resolving refs to CTEs by adding CTEs to the aliasMap and indexing
its columns when resolving catalog references.

Fix #3219

* feat(compiler): Support subqueries in the FROM clause

issue #2989, #2400 and probably others

* chore(endtoend): Bump version in cte_resolve_ref to 1.26.0 to appease CI

---------

Co-authored-by: Simon Klee <hello@simonklee.dk>
@kyleconroy kyleconroy added this to the 1.27.0 milestone Apr 4, 2024
Jille added a commit to Jille/sqlc that referenced this issue Apr 4, 2024
Jille added a commit to Jille/sqlc that referenced this issue Apr 4, 2024
simonklee pushed a commit to simonklee/sqlc that referenced this issue Apr 9, 2024
simonklee pushed a commit to simonklee/sqlc that referenced this issue Apr 9, 2024
lisitsky pushed a commit to lisitsky/sqlc that referenced this issue Jun 21, 2024
* fix(resolve): fix resolving reference to CTEs

Fixed resolving refs to CTEs by adding CTEs to the aliasMap and indexing
its columns when resolving catalog references.

Fix sqlc-dev#3219

* feat(compiler): Support subqueries in the FROM clause

issue sqlc-dev#2989, sqlc-dev#2400 and probably others

---------

Co-authored-by: Simon Klee <hello@simonklee.dk>
lisitsky pushed a commit to lisitsky/sqlc that referenced this issue Jun 21, 2024
…sqlc-dev#3310)

* fix(resolve): fix resolving reference to CTEs

Fixed resolving refs to CTEs by adding CTEs to the aliasMap and indexing
its columns when resolving catalog references.

Fix sqlc-dev#3219

* feat(compiler): Support subqueries in the FROM clause

issue sqlc-dev#2989, sqlc-dev#2400 and probably others

* chore(endtoend): Bump version in cte_resolve_ref to 1.26.0 to appease CI

---------

Co-authored-by: Simon Klee <hello@simonklee.dk>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage New issues that hasn't been reviewed
Projects
None yet
Development

No branches or pull requests

3 participants