Skip to content

Commit

Permalink
fix(schema): field is only unique when there is one unique index (#5974)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nomango authored Jan 11, 2023
1 parent 2bc9137 commit baf1afa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
7 changes: 5 additions & 2 deletions schema/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -129,7 +133,6 @@ func parseFieldIndexes(field *Field) (indexes []Index, err error) {
}

if (k == "UNIQUEINDEX") || settings["UNIQUE"] != "" {
field.Unique = true
settings["CLASS"] = "UNIQUE"
}

Expand Down
11 changes: 7 additions & 4 deletions schema/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit baf1afa

Please sign in to comment.