From 61b4c31236a8f9792c94240ddb4e236f21bbb9ff Mon Sep 17 00:00:00 2001 From: labulakalia Date: Mon, 14 Mar 2022 21:47:59 +0800 Subject: [PATCH] fix when index name is "type", parseFieldIndexes will set index TYPE is "TYPE" (#5155) * fix index name is type, parseFieldIndexes will set index TYPE is "TYPE" * check TYPE empty --- schema/index.go | 11 ++++++----- schema/index_test.go | 6 ++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/schema/index.go b/schema/index.go index 5f775f30..16d096b7 100644 --- a/schema/index.go +++ b/schema/index.go @@ -89,11 +89,12 @@ func parseFieldIndexes(field *Field) (indexes []Index) { k := strings.TrimSpace(strings.ToUpper(v[0])) if k == "INDEX" || k == "UNIQUEINDEX" { var ( - name string - tag = strings.Join(v[1:], ":") - idx = strings.Index(tag, ",") - settings = ParseTagSetting(tag, ",") - length, _ = strconv.Atoi(settings["LENGTH"]) + name string + tag = strings.Join(v[1:], ":") + idx = strings.Index(tag, ",") + tagSetting = strings.Join(strings.Split(tag, ",")[1:], ",") + settings = ParseTagSetting(tagSetting, ",") + length, _ = strconv.Atoi(settings["LENGTH"]) ) if idx == -1 { diff --git a/schema/index_test.go b/schema/index_test.go index bc6bb8b6..3c4582bb 100644 --- a/schema/index_test.go +++ b/schema/index_test.go @@ -18,6 +18,7 @@ type UserIndex struct { Age int64 `gorm:"index:profile,expression:ABS(age),option:WITH PARSER parser_name"` OID int64 `gorm:"index:idx_id;index:idx_oid,unique"` MemberNumber string `gorm:"index:idx_id,priority:1"` + Name7 string `gorm:"index:type"` } func TestParseIndex(t *testing.T) { @@ -78,6 +79,11 @@ func TestParseIndex(t *testing.T) { Class: "UNIQUE", Fields: []schema.IndexOption{{Field: &schema.Field{Name: "OID"}}}, }, + "type": { + Name: "type", + Type: "", + Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name7"}}}, + }, } indices := user.ParseIndexes()