Skip to content

Commit baf1afa

Browse files
authored
fix(schema): field is only unique when there is one unique index (#5974)
1 parent 2bc9137 commit baf1afa

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

schema/index.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ func (schema *Schema) ParseIndexes() map[string]Index {
6565
}
6666
}
6767
}
68-
68+
for _, index := range indexes {
69+
if index.Class == "UNIQUE" && len(index.Fields) == 1 {
70+
index.Fields[0].Field.Unique = true
71+
}
72+
}
6973
return indexes
7074
}
7175

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

131135
if (k == "UNIQUEINDEX") || settings["UNIQUE"] != "" {
132-
field.Unique = true
133136
settings["CLASS"] = "UNIQUE"
134137
}
135138

schema/index_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func TestParseIndex(t *testing.T) {
6565
"idx_name": {
6666
Name: "idx_name",
6767
Class: "UNIQUE",
68-
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name2"}}},
68+
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name2", Unique: true}}},
6969
},
7070
"idx_user_indices_name3": {
7171
Name: "idx_user_indices_name3",
@@ -81,7 +81,7 @@ func TestParseIndex(t *testing.T) {
8181
"idx_user_indices_name4": {
8282
Name: "idx_user_indices_name4",
8383
Class: "UNIQUE",
84-
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name4"}}},
84+
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name4", Unique: true}}},
8585
},
8686
"idx_user_indices_name5": {
8787
Name: "idx_user_indices_name5",
@@ -102,12 +102,12 @@ func TestParseIndex(t *testing.T) {
102102
},
103103
"idx_id": {
104104
Name: "idx_id",
105-
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "MemberNumber"}}, {Field: &schema.Field{Name: "OID"}}},
105+
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "MemberNumber"}}, {Field: &schema.Field{Name: "OID", Unique: true}}},
106106
},
107107
"idx_oid": {
108108
Name: "idx_oid",
109109
Class: "UNIQUE",
110-
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "OID"}}},
110+
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "OID", Unique: true}}},
111111
},
112112
"type": {
113113
Name: "type",
@@ -168,6 +168,9 @@ func TestParseIndex(t *testing.T) {
168168
if rf.Field.Name != ef.Field.Name {
169169
t.Fatalf("index field should equal, expects %v, got %v", rf.Field.Name, ef.Field.Name)
170170
}
171+
if rf.Field.Unique != ef.Field.Unique {
172+
t.Fatalf("index field '%s' should equal, expects %v, got %v", rf.Field.Name, rf.Field.Unique, ef.Field.Unique)
173+
}
171174

172175
for _, name := range []string{"Expression", "Sort", "Collate", "Length"} {
173176
if reflect.ValueOf(ef).FieldByName(name).Interface() != reflect.ValueOf(rf).FieldByName(name).Interface() {

0 commit comments

Comments
 (0)