Skip to content

Strange error: unsupported reference type: <nil> #2786

Closed
@andrei-dascalu

Description

@andrei-dascalu

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/700814035f1998de27a80e0203e9657a

Database 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

https://play.sqlc.dev/p/296540930cfa87073b096c229df93152bae688b9c997bc70f94b4264928d5464

What operating system are you using?

macOS

What database engines are you using?

MySQL

What type of code are you generating?

Go

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions