Skip to content

Non-nullable Postgres enum type generates non-compiling code #313

@ballistic-booger

Description

@ballistic-booger

When using sqlc-gen-csharp with a Postgres enum that is declared as non-nullable in the schema, the generated C# code does not compile.
Specifically, the generated class references .Value.Stringify() on the enum property, which is invalid when the enum type is not nullable.

Generated cs:

public class CreateBoogerSampleArgs
{
    public required int Id { get; init; }
    public required Booger BoogerType { get; init; }
};

public async Task CreateBoogerSample(CreateBoogerSampleArgs args)
{
    var queryParams = new Dictionary<string, object?>();
    queryParams.Add("id", args.Id);
    queryParams.Add("booger_type", args.BoogerType.Value.Stringify());
    ...
}

query.sql:

-- name: CreateBoogerSample :exec
INSERT INTO booger_sample (id, type)
VALUES (@id, @booger_type);

schema.sql:

CREATE TYPE booger AS ENUM (
    'fresh',
    'old'
);

CREATE TABLE booger_sample (
    id INT NOT NULL,
    type booger NOT NULL
);

Expected Behavior
For a non-nullable enum column, the generated code should not reference .Value. Instead, it should call .Stringify() directly on the enum property.
Correct code should look like:

queryParams.Add("booger_type", args.BoogerType.Stringify());

Environment
sqlc docker image: latest
Database: PostgreSQL
Target: C# (Dapper + Npgsql)
sqlc-gen-csharp: v0.21.2

  • 98990c2a46b0e315c7f062f5643e3404ed4150a5ef2808c47a4e10084443506e

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions