From a4db856cd48d70c157c9a5e525f410f4e837ee89 Mon Sep 17 00:00:00 2001 From: Ivan Kolombet Date: Fri, 28 Mar 2025 14:58:12 +0300 Subject: [PATCH] fix(orm): create sql.NullInt16 column as smallint, sql.NullInt32 as integer Currently, sql.NullInt16 and sql.NullInt32 are created as JSONB columns. This fixes that. --- orm/table.go | 6 ++++++ orm/table_create_test.go | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/orm/table.go b/orm/table.go index 29c1c0c3..0ea3e31b 100644 --- a/orm/table.go +++ b/orm/table.go @@ -45,6 +45,8 @@ var ( nullBoolType = reflect.TypeOf((*sql.NullBool)(nil)).Elem() nullFloatType = reflect.TypeOf((*sql.NullFloat64)(nil)).Elem() nullIntType = reflect.TypeOf((*sql.NullInt64)(nil)).Elem() + nullInt32Type = reflect.TypeOf((*sql.NullInt32)(nil)).Elem() + nullInt16Type = reflect.TypeOf((*sql.NullInt16)(nil)).Elem() nullStringType = reflect.TypeOf((*sql.NullString)(nil)).Elem() jsonRawMessageType = reflect.TypeOf((*json.RawMessage)(nil)).Elem() ) @@ -1243,6 +1245,10 @@ func sqlType(typ reflect.Type) string { return pgTypeDoublePrecision case nullIntType: return pgTypeBigint + case nullInt32Type: + return pgTypeInteger + case nullInt16Type: + return pgTypeSmallint case nullStringType: return pgTypeText case jsonRawMessageType: diff --git a/orm/table_create_test.go b/orm/table_create_test.go index fe15b730..3f0130c6 100644 --- a/orm/table_create_test.go +++ b/orm/table_create_test.go @@ -33,6 +33,8 @@ type CreateTableModel struct { NullBool sql.NullBool NullFloat64 sql.NullFloat64 NullInt64 sql.NullInt64 + NullInt32 sql.NullInt32 + NullInt16 sql.NullInt16 NullString sql.NullString Slice []int SliceArray []int `pg:",array"` @@ -96,7 +98,7 @@ var _ = Describe("CreateTable", func() { q := NewQuery(nil, &CreateTableModel{}) s := createTableQueryString(q, nil) - Expect(s).To(Equal(`CREATE TABLE "create_table_models" ("id" bigserial, "serial" bigint, "int8" smallint, "uint8" smallint, "int16" smallint, "uint16" integer, "int32" integer, "uint32" bigint, "int64" bigint, "uint64" bigint, "float32" real, "float64" double precision, "decimal" decimal(10,10), "byte_slice" bytea, "byte_array" bytea, "string" text DEFAULT 'D''Angelo', "varchar" varchar(500), "time" timestamptz DEFAULT now(), "duration" bigint, "not_null" bigint NOT NULL, "null_bool" boolean, "null_float64" double precision, "null_int64" bigint, "null_string" text, "slice" jsonb, "slice_array" bigint[], "map" jsonb, "map_hstore" hstore, "struct" jsonb, "struct_ptr" jsonb, "unique" bigint UNIQUE, "unique_field1" bigint, "unique_field2" bigint, "json_raw_message" jsonb, PRIMARY KEY ("id"), UNIQUE ("unique"), UNIQUE ("unique_field1", "unique_field2"))`)) + Expect(s).To(Equal(`CREATE TABLE "create_table_models" ("id" bigserial, "serial" bigint, "int8" smallint, "uint8" smallint, "int16" smallint, "uint16" integer, "int32" integer, "uint32" bigint, "int64" bigint, "uint64" bigint, "float32" real, "float64" double precision, "decimal" decimal(10,10), "byte_slice" bytea, "byte_array" bytea, "string" text DEFAULT 'D''Angelo', "varchar" varchar(500), "time" timestamptz DEFAULT now(), "duration" bigint, "not_null" bigint NOT NULL, "null_bool" boolean, "null_float64" double precision, "null_int64" bigint, "null_int32" integer, "null_int16" smallint, "null_string" text, "slice" jsonb, "slice_array" bigint[], "map" jsonb, "map_hstore" hstore, "struct" jsonb, "struct_ptr" jsonb, "unique" bigint UNIQUE, "unique_field1" bigint, "unique_field2" bigint, "json_raw_message" jsonb, PRIMARY KEY ("id"), UNIQUE ("unique"), UNIQUE ("unique_field1", "unique_field2"))`)) }) It("creates new table without primary key", func() {