Invalid UUID type is generated for null column with foreign key
Version
1.27.0
What happened?
Generated models.go
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.27.0
package repository
import (
"github.com/google/uuid"
)
type Item struct {
ID uuid.UUID `db:"id"`
ParentID uuid.UUID `db:"parent_id"`
Name string `db:"name"`
}
I expect sqlc to generate ParentID *uuid.UUID, because this column is nullable
Relevant log output
No response
Database schema
CREATE TABLE IF NOT EXISTS items
(
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
parent_id UUID REFERENCES items (id) NULL,
name TEXT NOT NULL
);
SQL queries
-- name: CreateItem :exec
INSERT INTO items (id, parent_id, name)
VALUES (@id,
@parent_id,
@name);
Configuration
version: "2"
sql:
- engine: "postgresql"
queries: "query.sql"
schema: "schema.sql"
gen:
go:
package: "repository"
out: "."
sql_package: "pgx/v5"
emit_all_enum_values: true
emit_db_tags: true
emit_interface: true
emit_json_tags: true
emit_prepared_queries: true
emit_enum_valid_method: true
emit_empty_slices: true
emit_params_struct_pointers: true
emit_result_struct_pointers: true
emit_pointers_for_null_types: true
json_tags_case_style: "snake"
query_parameter_limit: 2
overrides:
- db_type: "uuid"
go_type: "github.com/google/uuid.UUID"
- db_type: "uuid"
go_type: "github.com/google/uuid.UUID"
nullable: true
Playground URL
https://play.sqlc.dev/p/5121c5319cdf6954e4bbfd00e098390d567401afe8cd5418ce9fb4217ac902c7
What operating system are you using?
macOS
What database engines are you using?
PostgreSQL
What type of code are you generating?
Go
I am having a similar issue with Sqlite3 where I have the following override:
emit_pointers_for_null_types: true
overrides:
- column: "*.*_id"
go_type: "github.com/kyren223/eko/pkg/snowflake.ID"
I expect that if the column is not annotated with NOT NULL it should generate a pointer to that type, and only if it's NOT NULL it will generate the normal type.
EDIT: If I don't specify a type, it correctly uses int64 and *int64 bot if I override then it doesn't work
overrides:
- db_type: "uuid"
go_type: "github.com/google/uuid.UUID"
- db_type: "uuid"
go_type:
import: "github.com/google/uuid"
type: "UUID"
pointer: true
nullable: true
https://play.sqlc.dev/p/773268972b4140b3d2aca91fd2d1adcda771c84cb7f177008894b8bf78055c35 I found a workaround, but I am not sure that it solves all cases
I am using Sqlite3 which has only 5 basic types which means I need to use column instead of db_type, and nullable doesn't work on column types. Maybe this should be a separate issue? it's the same problem but different context
This is what worked for me:
emit_pointers_for_null_types: true
overrides:
- db_type: "uuid"
go_type: "github.com/google/uuid.UUID"
- db_type: "uuid"
nullable: true
go_type: "*github.com/google/uuid.UUID"