Repository navigation
Description
Version
1.22.0
What happened?
I am trying to generate queries in a local project and it results in this error
unsupported reference type: <nil>
The reason for this is unclear in the sense that the exact same structure/queries works fine in Playground. It only appears locally on my Mac, running go 1.21.1
Now, the code is generated - despite the error showing.
I've tried installing sqlc both with go install and brew, made no difference. I can't figure out what's different between what runs in playground and what runs locally. Or maybe the playground simply ignores that output?
The real issue comes when trying to run the function generated => sql: expected 3 arguments, got 2
This is particularly strange because it seems it doesn't recognize arguments with the same name which are part of the same .... scope? I think, because I wrote more complex cases where arguments like calendarIdKey were reused in different parts of the same INSERT and they were fine. However, the timezone argument here is taken separately when part of the CASE test vs when it's used to provide the value in ELSE.
Relevant log output
`unsupported reference type: <nil>`
With debug: https://gist.github.com/andrei-dascalu/700814035f1998de27a80e0203e9657aDatabase schema
CREATE TABLE `Calendar` (
`Id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`Relation` bigint(20) unsigned NOT NULL,
`CalendarName` longblob NOT NULL,
`Title` longblob NOT NULL,
`Description` longblob NOT NULL,
`Timezone` varchar(50) NOT NULL,
`UniqueKey` varchar(50) NOT NULL,
`IdKey` varchar(50) NOT NULL,
`MainCalendar` enum('true','false') NOT NULL DEFAULT 'false',
`CreateDate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ModifyDate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`Id`),
KEY `Relation` (`Relation`),
KEY `UniqueKey` (`UniqueKey`),
KEY `IdKey` (`IdKey`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;
CREATE TABLE `Event` (
`Id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`Relation` bigint(20) unsigned NOT NULL,
`CalendarReference` bigint(20) unsigned NOT NULL,
`UniqueKey` varchar(50) NOT NULL,
`EventName` longblob NOT NULL,
`Description` longblob NOT NULL,
`Location` varchar(500) NOT NULL,
`Timezone` varchar(50) NOT NULL,
`IdKey` varchar(48) DEFAULT NULL,
PRIMARY KEY (`Id`),
KEY `Relation` (`Relation`),
KEY `CalendarReference` (`CalendarReference`),
KEY `UniqueKey` (`UniqueKey`),
KEY `IdKey` (`IdKey`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;SQL queries
-- name: AddEvent :execlastid
INSERT INTO `Event` (
Timezone
) VALUES (
(CASE WHEN sqlc.arg("Timezone") = "calendar" THEN (SELECT cal.Timezone FROM Calendar cal WHERE cal.IdKey = sqlc.arg("calendarIdKey")) ELSE sqlc.arg("Timezone") END)
);Configuration
No response
Playground URL
What operating system are you using?
macOS
What database engines are you using?
MySQL
What type of code are you generating?
Go
Activity
andrei-dascalu commentedon Sep 28, 2023
if I change
ELSE sqlc.arg("Timezone") ENDtoELSE "test" ENDthen it works. It looks like it won't process the second arg even if it's same as what's used already in the CASE. Still, in Playground the exact same setup works so I'm not sure what I can do to further debug what's going on.andrei-dascalu commentedon Sep 28, 2023
Updated desc to reflect issues:
1 - the error that appears when running
generatewhich doesn't seem to break the generation of code2 - the fact that trying to run the generated function results in an SQL error that complains about expecting 3 arguments and only 2 provided.
andrei-dascalu commentedon Sep 28, 2023
On a second look, it looks more like arguments are simply not accepted as part of ELSE. It doesn't stop the code generation, but if I change the name of the argument, the argument mentioned in ELSE is not generated as part of the parameter struct.
kyleconroy commentedon Sep 28, 2023
First, I noticed that your
sqlc.jsonhassql_packageset topgx/v5, which has no effect when the engine is set tomysql. We should probably emit an warning in that case. I don't think this has any effect on underlying error.The issue is that sqlc isn't generated the correct number of parameters for your query. It should be three, not two
Can you confirm that the manually updated Go code works?
orisano commentedon Sep 28, 2023
In a case like
INSERT > VALUES > CASE WHEN ELSE > paramter, find_params will return a paramRef with parent nil.sqlc/internal/compiler/find_params.go
Lines 56 to 219 in b2e42f8
resolveCatalogRefs ignores paramRefs with nil parent and outputs error messages like the one in the description
sqlc/internal/compiler/resolve.go
Line 603 in b2e42f8
There are two ways to solve this problem.
I think it is better to deal with 1 first, and then deal with 2.
This is fundamentally the same problem as #2748.
orisano commentedon Sep 28, 2023
This is a compiler problem that occurs with all engines, not just MySQL.
andrei-dascalu commentedon Sep 29, 2023
@kyleconroy indeed, manually adding the same parameter in the list work.
As a workaround for my current situation I used a param with different name that I'm assigning the same value. As a secondary issue, I had to write the workaround SQL as
Without the specific convert, it would complain that it can't figure the type
kyleconroy commentedon Oct 12, 2023
Fix has been merged, will be included in v1.23.0