Skip to content

Support for Nullable Types in Generated Structs for Expression Columns #2765

Open
@midsbie

Description

@midsbie

What do you want to change?

Currently, sqlc generates Go structs with non-nullable types for fields that are derived from SQL expressions, even when these fields can actually be NULL based on the SQL query logic.

Consider the following SQL query:

-- :name GetNothing :one
SELECT GREATEST(NULL)::TIMESTAMP AS updated_at

The current version of sqlc generates the following Go struct:

type GetNothing struct {
	UpdatedAt time.Time
}

Desired Output

I would like to have the ability to configure sqlc so that it generates nullable types in such scenarios. The desired Go struct would look like:

type GetNothing struct {
	UpdatedAt *time.Time // or sql.NullTime, depending on configuration
}

Proposed Solution

Ideally extend the sqlc comment syntax to enable specifying nullable types on a per query column basis. Example:

SELECT GREATEST(NULL)::TIMESTAMP AS updated_at, -- :nullable
              col1, col2,
              NULL::TEXT AS always_null, -- :nullable
              ...

What database engines need to be changed?

PostgreSQL, MySQL

What programming language backends need to be changed?

Go, Python, Kotlin

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions