From 8a0af58cc5315f2b8562cac2c06fbdea05cd0e03 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 20 Jun 2024 20:19:31 +0800 Subject: [PATCH] fix map fields with clickhouse driver --- schema/schema.go | 2 +- schema/schema_test.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/schema/schema.go b/schema/schema.go index 57bab7fd..db236797 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -338,7 +338,7 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam if _, embedded := schema.cacheStore.Load(embeddedCacheKey); !embedded { for _, field := range schema.Fields { - if field.DataType == "" && (field.Creatable || field.Updatable || field.Readable) { + if field.DataType == "" && field.GORMDataType == "" && (field.Creatable || field.Updatable || field.Readable) { if schema.parseRelation(field); schema.err != nil { return schema, schema.err } else { diff --git a/schema/schema_test.go b/schema/schema_test.go index 45e152e9..a7115f60 100644 --- a/schema/schema_test.go +++ b/schema/schema_test.go @@ -19,6 +19,22 @@ func TestParseSchema(t *testing.T) { checkUserSchema(t, user) } +func TestParseSchemaWithMap(t *testing.T) { + type User struct { + tests.User + Attrs map[string]string `gorm:"type:Map(String,String);"` + } + + user, err := schema.Parse(&User{}, &sync.Map{}, schema.NamingStrategy{}) + if err != nil { + t.Fatalf("failed to parse user with map, got error %v", err) + } + + if field := user.FieldsByName["Attrs"]; field.DataType != "Map(String,String)" { + t.Errorf("failed to parse user field Attrs") + } +} + func TestParseSchemaWithPointerFields(t *testing.T) { user, err := schema.Parse(&User{}, &sync.Map{}, schema.NamingStrategy{}) if err != nil {