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?