4

I have the generated code for the given query

INSERT INTO "users" (
    username,
    name,
    surname,
    email,
    hashed_password,
    role
) VALUES (
    $1, $2, $3, $4, $5, $6
) RETURNING "id", "username";

which looks like this

type CreateUserParams struct {
    Username       string      `json:"username"`
    Name           pgtype.Text `json:"name"`
    Surname        pgtype.Text `json:"surname"`
    Email          string      `json:"email"`
    HashedPassword string      `json:"hashed_password"`
    Role           UserRole    `json:"role"`
}

How can I change Name pgtype.Text into golang string? currently my sqlc.yaml file looks like following

version: "2"
sql:
  - schema: "db/migrations/users"
    queries: "db/queries"
    engine: "postgresql"
    gen:
      go:
        package: "internal"
        out: "db/internal/"
        sql_package: "pgx/v5"
        output_files_suffix: "_gen"
        emit_json_tags: true
        emit_interface: true
        emit_empty_slices: true
        overrides:
          - db_type: "timestamptz"
            go_type: "time.Time"
          - db_type: "uuid"
            go_type: "github.com/google/uuid.UUID"
          - db_type: "text"
            go_type:
              type: "string"

The schema for me looks like following


CREATE TABLE users (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    username VARCHAR(50) UNIQUE NOT NULL,
    name VARCHAR(100),
    surname VARCHAR(100),
    email VARCHAR(150) UNIQUE NOT NULL CHECK (email ~* '^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$'),
    hashed_password VARCHAR(1024) NOT NULL,
    role user_role NOT NULL DEFAULT 'USER',
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

Here name and surname can be NULL based on schema. Now as @kozmo pointed, because these columns can be null, it is generating pgtype.Text instead of string. But I want to create string in every scenarios. How can I do that?

Mayukh Sarkar
  • 2,289
  • 1
  • 14
  • 38

1 Answers1

1

I guess, that your "users" schema file looks like

create table users
(
    id              uuid,
    username        text,
    name            text,
);

a sqlc generate

type User struct {
    ID       pgtype.UUID `json:"id"`
    Username pgtype.Text `json:"username"`
    Name     pgtype.Text `json:"name"`
}

if add constrain not null

create table users
(
    id              uuid,
    username        text,
    name            text not null --- 
);

generated code looks like

type User struct {
    ID       pgtype.UUID `json:"id"`
    Username pgtype.Text `json:"username"`
    Name     string      `json:"name"`
}
kozmo
  • 4,024
  • 3
  • 30
  • 48
  • Yes..for the values I am keeping `NOT NULL`, it is generating as strings but for those it is not mentioned, `pgtype.Text` is coming. I want to however make it string for everything. – Mayukh Sarkar Aug 08 '23 at 08:14
  • I did not find a way to generate `nullable` sql column to field with direct (no-poinert) type. Othe way, add `pointer: true` parameter to generate field with `*string` type. – kozmo Aug 08 '23 at 15:39