Skip to content

null pointer overrides not working #814

Open

Description

@mvrhov

I'm not sure and I might be missing something, but null overrides at least for timestamp to *time.Time, doesn't seem to wok, text, to *string works.

I've also tried with timestamptz, timestamp(0), timestamp without time zone I'm unable to debug this as the go compiler fails when building debug version in Goland with OOM.. it seems that 8G of free ram is not enough.

edit: , jsonb to *json.RawMessage kinda works.. it generates two imports "encoding/json" and json "encoding/json"

version: "1"
packages:
  - name: "db"
#....
overrides:
  - db_type: "timestamp"
    go_type:
      import: "time"
      package: "time"
      type: "Time"
      pointer: true
    nullable: true
  - db_type: "jsonb"
    go_type:
      import: "encoding/json"
      package: "json"
      type: "RawMessage"
      pointer: true
    nullable: true
CREATE TABLE office (
    id_office bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,

    created_at timestamp(0) DEFAULT now() NOT NULL,
    updated_at timestamp(0),
);

-- name: ListOffices :many
SELECT * FROM office
ORDER BY name;

This always generates

type Office struct {
	IDOffice    int64
	CreatedAt   time.Time
	UpdatedAt   sql.NullTime
}

Activity

  1. changed the title [-]null overrides `timestamp` to `*time.Time` not working[/-] [+]null pointer overrides not working[/+] on Nov 25, 2020
  2. added
    bugSomething isn't working
    documentationImprovements or additions to documentation
    and removed on Jan 25, 2021
  3. mvrhov commented on Feb 9, 2021

    @mvrhov
    Author

    Meh.. this seems to somewhat work but it's tedious and almost impossible to guess.

      - db_type: "pg_catalog.timestamp"
        go_type:
          import: "time"
          type: "Time"
          pointer: true
        nullable: true
    
      - db_type: "pg_catalog.int2"
        go_type:
          type: "int16"
          pointer: true
        nullable: true
      - db_type: "pg_catalog.int4"
        go_type:
          type: "int"
          pointer: true
        nullable: true
      - db_type: "pg_catalog.int8"
        go_type:
          type: "int64"
          pointer: true
        nullable: true
      - db_type: "pg_catalog.bool"
        go_type:
          type: "bool"
          pointer: true
        nullable: true
      - db_type: "pg_catalog.float4"
        go_type:
          type: "float32"
          pointer: true
        nullable: true
      - db_type: "pg_catalog.float8"
        go_type:
          type: "float64"
          pointer: true
        nullable: true
  4. jskelcy commented on Mar 19, 2021

    @jskelcy
    Contributor

    hmm I am trying to use this and its not working for me. Anything tricky you did here @mvrhov? In particular, I cant get timestamps to work.

  5. mvrhov commented on Mar 22, 2021

    @mvrhov
    Author

    @jskelcy Are you saying, that the config one post above yours doesn't work for you?

  6. jskelcy commented on Mar 22, 2021

    @jskelcy
    Contributor

    I should have updated here. But I found my issue, essentially the params object which are generated for a function dont seem to respect overrides in some cases. The tables themselves were being generated with the right types. I was able to come up with a workaround, I outline it in sort of an exploratory stream of consciousness here: #950. I will be updating the ticket with a summary today.

    Thanks for checking @mvrhov

  7. ptman commented on Mar 7, 2022

    @ptman

    I'm not having success with using *string instead of NullString:

    overrides:
      - db_type: 'pg_catalog.text'
        go_type:
          type: 'string'
          pointer: true
        nullable: true
      - db_type: 'pg_catalog.text'
        go_type:
          type: 'string'
          pointer: false
        nullable: false
  8. mvrhov commented on Mar 8, 2022

    @mvrhov
    Author

    db_type: 'pg_catalog.text' should be db_type: 'text'. Yeah I know it's a mess and mix of both depending on the type.

  9. kyleconroy commented on Sep 22, 2023

    @kyleconroy
    Collaborator

    Tracking documentation improvements for this #2762

  10. lightSoulDev commented on Jul 6, 2025

    @lightSoulDev

    This issue appears to be resolved in current versions, but since it remains open and is still discoverable through search engines. I wanted to summarize and document this workaround for anyone facing similar issues and suggest to close the issue if it is no longer relevant and this feature is already documented in the sqlc documentation.

    Issue

    When using emit_pointers_for_null_types: true with type overrides in sqlc, nullable columns don't generate pointer types as expected for cerain types. For example: pgtype.Timestamptz is generated for nullable timestamptz fields.

    Solution

    Adding an additional override entry with db_type.nullable: true and go_type.pointer: true resolved the issue:

    ...
    emit_pointers_for_null_types: true
    overrides:
      - db_type: "timestamptz"
        go_type:
          import: "time"
          type: "Time"
      - db_type: "timestamptz"
        nullable: true
        go_type:
          import: "time"
          type: "Time"
          pointer: true

    Example

    Given this SQL schema:

    CREATE TABLE "user" (
        ...
        created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
        deleted_at TIMESTAMPTZ DEFAULT NULL
    );

    The generated Go struct now correctly uses pointer types for nullable fields:

    type User struct {
        ...
        CreatedAt time.Time   `json:"created_at"`
        DeletedAt *time.Time  `json:"deleted_at"`
    }

    Without the nullable override, DeletedAt would be generated as pgtype.Timestamptz instead of *time.Time.

    type User struct {
        ...
        CreatedAt time.Time           `json:"created_at"`
        DeletedAt pgtype.Timestamptz  `json:"deleted_at"` 
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    🔧 golangbugSomething isn't workingdocumentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions