diff --git a/schema/index.go b/schema/index.go index a0a71d2c..fb7ea501 100644 --- a/schema/index.go +++ b/schema/index.go @@ -1,6 +1,7 @@ package schema import ( + "sort" "strconv" "strings" ) @@ -20,6 +21,7 @@ type IndexOption struct { Sort string // DESC, ASC Collate string Length int + priority int } // ParseIndexes parse schema indexes @@ -43,7 +45,12 @@ func (schema *Schema) ParseIndexes() map[string]Index { if idx.Comment == "" { idx.Comment = index.Comment } + idx.Fields = append(idx.Fields, index.Fields...) + sort.Slice(idx.Fields, func(i, j int) bool { + return idx.Fields[i].priority < idx.Fields[j].priority + }) + indexes[index.Name] = idx } } @@ -101,6 +108,11 @@ func parseFieldIndexes(field *Field) (indexes []Index) { settings["CLASS"] = "UNIQUE" } + priority, err := strconv.Atoi(settings["PRIORITY"]) + if err != nil { + priority = 10 + } + indexes = append(indexes, Index{ Name: name, Class: settings["CLASS"], @@ -113,6 +125,7 @@ func parseFieldIndexes(field *Field) (indexes []Index) { Sort: settings["SORT"], Collate: settings["COLLATE"], Length: length, + priority: priority, }}, }) } diff --git a/schema/index_test.go b/schema/index_test.go index f6c3d247..dc1fb43b 100644 --- a/schema/index_test.go +++ b/schema/index_test.go @@ -17,7 +17,7 @@ type UserIndex struct { Name6 int64 `gorm:"index:profile,comment:hello \\, world,where:age > 10"` Age int64 `gorm:"index:profile,expression:ABS(age)"` OID int64 `gorm:"index:idx_id;index:idx_oid,unique"` - MemberNumber string `gorm:"index:idx_id"` + MemberNumber string `gorm:"index:idx_id,priority:1"` } func TestParseIndex(t *testing.T) { @@ -29,18 +29,19 @@ func TestParseIndex(t *testing.T) { results := map[string]schema.Index{ "idx_user_indices_name": { Name: "idx_user_indices_name", - Fields: []schema.IndexOption{{}}, + Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name"}}}, }, "idx_name": { Name: "idx_name", Class: "UNIQUE", - Fields: []schema.IndexOption{{}}, + Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name2"}}}, }, "idx_user_indices_name3": { Name: "idx_user_indices_name3", Type: "btree", Where: "name3 != 'jinzhu'", Fields: []schema.IndexOption{{ + Field: &schema.Field{Name: "Name3"}, Sort: "desc", Collate: "utf8", Length: 10, @@ -49,31 +50,32 @@ func TestParseIndex(t *testing.T) { "idx_user_indices_name4": { Name: "idx_user_indices_name4", Class: "UNIQUE", - Fields: []schema.IndexOption{{}}, + Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name4"}}}, }, "idx_user_indices_name5": { Name: "idx_user_indices_name5", Class: "FULLTEXT", Comment: "hello , world", Where: "age > 10", - Fields: []schema.IndexOption{{}}, + Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name5"}}}, }, "profile": { Name: "profile", Comment: "hello , world", Where: "age > 10", - Fields: []schema.IndexOption{{}, { + Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name6"}}, { + Field: &schema.Field{Name: "Age"}, Expression: "ABS(age)", }}, }, "idx_id": { Name: "idx_id", - Fields: []schema.IndexOption{{}, {}}, + Fields: []schema.IndexOption{{Field: &schema.Field{Name: "MemberNumber"}}, {Field: &schema.Field{Name: "OID"}}}, }, "idx_oid": { Name: "idx_oid", Class: "UNIQUE", - Fields: []schema.IndexOption{{}}, + Fields: []schema.IndexOption{{Field: &schema.Field{Name: "OID"}}}, }, } @@ -96,6 +98,10 @@ func TestParseIndex(t *testing.T) { for idx, ef := range result.Fields { rf := v.Fields[idx] + if rf.Field.Name != ef.Field.Name { + t.Fatalf("index field should equal, expects %v, got %v", rf.Field.Name, ef.Field.Name) + } + for _, name := range []string{"Expression", "Sort", "Collate", "Length"} { if reflect.ValueOf(ef).FieldByName(name).Interface() != reflect.ValueOf(rf).FieldByName(name).Interface() { t.Errorf( diff --git a/schema/relationship.go b/schema/relationship.go index 0967f8c8..91c2ca8d 100644 --- a/schema/relationship.go +++ b/schema/relationship.go @@ -130,7 +130,7 @@ func (schema *Schema) buildPolymorphicRelation(relation *Relationship, field *Fi PolymorphicID: relation.FieldSchema.FieldsByName[polymorphic+"ID"], } - if value, ok := field.TagSettings["POLYMORPHIC_VALUE"]; ok { + if value, ok := field.TagSettings["POLYMORPHICVALUE"]; ok { relation.Polymorphic.Value = strings.TrimSpace(value) } diff --git a/tests/named_polymorphic_test.go b/tests/named_polymorphic_test.go index cbe236b5..956f3a7e 100644 --- a/tests/named_polymorphic_test.go +++ b/tests/named_polymorphic_test.go @@ -9,8 +9,8 @@ import ( type Hamster struct { Id int Name string - PreferredToy Toy `gorm:"polymorphic:Owner;polymorphic_value:hamster_preferred"` - OtherToy Toy `gorm:"polymorphic:Owner;polymorphic_value:hamster_other"` + PreferredToy Toy `gorm:"polymorphic:Owner;polymorphicValue:hamster_preferred"` + OtherToy Toy `gorm:"polymorphic:Owner;polymorphicValue:hamster_other"` } func TestNamedPolymorphic(t *testing.T) { diff --git a/utils/tests/models.go b/utils/tests/models.go index 021b0229..2c5e71c0 100644 --- a/utils/tests/models.go +++ b/utils/tests/models.go @@ -24,8 +24,8 @@ type User struct { ManagerID *uint Manager *User Team []User `gorm:"foreignkey:ManagerID"` - Languages []Language `gorm:"many2many:UserSpeak"` - Friends []*User `gorm:"many2many:user_friends"` + Languages []Language `gorm:"many2many:UserSpeak;"` + Friends []*User `gorm:"many2many:user_friends;"` Active bool }