diff --git a/schema/index.go b/schema/index.go index c29623ad..f5ac5dd2 100644 --- a/schema/index.go +++ b/schema/index.go @@ -65,7 +65,11 @@ func (schema *Schema) ParseIndexes() map[string]Index { } } } - + for _, index := range indexes { + if index.Class == "UNIQUE" && len(index.Fields) == 1 { + index.Fields[0].Field.Unique = true + } + } return indexes } @@ -129,7 +133,6 @@ func parseFieldIndexes(field *Field) (indexes []Index, err error) { } if (k == "UNIQUEINDEX") || settings["UNIQUE"] != "" { - field.Unique = true settings["CLASS"] = "UNIQUE" } diff --git a/schema/index_test.go b/schema/index_test.go index 1fe31cc1..890327de 100644 --- a/schema/index_test.go +++ b/schema/index_test.go @@ -65,7 +65,7 @@ func TestParseIndex(t *testing.T) { "idx_name": { Name: "idx_name", Class: "UNIQUE", - Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name2"}}}, + Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name2", Unique: true}}}, }, "idx_user_indices_name3": { Name: "idx_user_indices_name3", @@ -81,7 +81,7 @@ func TestParseIndex(t *testing.T) { "idx_user_indices_name4": { Name: "idx_user_indices_name4", Class: "UNIQUE", - Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name4"}}}, + Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name4", Unique: true}}}, }, "idx_user_indices_name5": { Name: "idx_user_indices_name5", @@ -102,12 +102,12 @@ func TestParseIndex(t *testing.T) { }, "idx_id": { Name: "idx_id", - Fields: []schema.IndexOption{{Field: &schema.Field{Name: "MemberNumber"}}, {Field: &schema.Field{Name: "OID"}}}, + Fields: []schema.IndexOption{{Field: &schema.Field{Name: "MemberNumber"}}, {Field: &schema.Field{Name: "OID", Unique: true}}}, }, "idx_oid": { Name: "idx_oid", Class: "UNIQUE", - Fields: []schema.IndexOption{{Field: &schema.Field{Name: "OID"}}}, + Fields: []schema.IndexOption{{Field: &schema.Field{Name: "OID", Unique: true}}}, }, "type": { Name: "type", @@ -168,6 +168,9 @@ func TestParseIndex(t *testing.T) { if rf.Field.Name != ef.Field.Name { t.Fatalf("index field should equal, expects %v, got %v", rf.Field.Name, ef.Field.Name) } + if rf.Field.Unique != ef.Field.Unique { + t.Fatalf("index field '%s' should equal, expects %v, got %v", rf.Field.Name, rf.Field.Unique, ef.Field.Unique) + } for _, name := range []string{"Expression", "Sort", "Collate", "Length"} { if reflect.ValueOf(ef).FieldByName(name).Interface() != reflect.ValueOf(rf).FieldByName(name).Interface() {