mirror of https://github.com/go-gorm/gorm.git
Fix create index
This commit is contained in:
parent
511bd66490
commit
d39bdc3513
|
@ -150,7 +150,7 @@ func (m Migrator) CreateTable(values ...interface{}) error {
|
||||||
|
|
||||||
for _, idx := range stmt.Schema.ParseIndexes() {
|
for _, idx := range stmt.Schema.ParseIndexes() {
|
||||||
if m.CreateIndexAfterCreateTable {
|
if m.CreateIndexAfterCreateTable {
|
||||||
tx.Migrator().CreateIndex(value, idx.Name)
|
defer tx.Migrator().CreateIndex(value, idx.Name)
|
||||||
} else {
|
} else {
|
||||||
createTableSQL += "INDEX ? ?,"
|
createTableSQL += "INDEX ? ?,"
|
||||||
values = append(values, clause.Expr{SQL: idx.Name}, tx.Migrator().(BuildIndexOptionsInterface).BuildIndexOptions(idx.Fields, stmt))
|
values = append(values, clause.Expr{SQL: idx.Name}, tx.Migrator().(BuildIndexOptionsInterface).BuildIndexOptions(idx.Fields, stmt))
|
||||||
|
|
|
@ -26,7 +26,7 @@ type IndexOption struct {
|
||||||
func (schema *Schema) ParseIndexes() map[string]Index {
|
func (schema *Schema) ParseIndexes() map[string]Index {
|
||||||
var indexes = map[string]Index{}
|
var indexes = map[string]Index{}
|
||||||
|
|
||||||
for _, field := range schema.FieldsByDBName {
|
for _, field := range schema.Fields {
|
||||||
if field.TagSettings["INDEX"] != "" || field.TagSettings["UNIQUE_INDEX"] != "" {
|
if field.TagSettings["INDEX"] != "" || field.TagSettings["UNIQUE_INDEX"] != "" {
|
||||||
for _, index := range parseFieldIndexes(field) {
|
for _, index := range parseFieldIndexes(field) {
|
||||||
idx := indexes[index.Name]
|
idx := indexes[index.Name]
|
||||||
|
@ -66,6 +66,10 @@ func parseFieldIndexes(field *Field) (indexes []Index) {
|
||||||
length, _ = strconv.Atoi(settings["LENGTH"])
|
length, _ = strconv.Atoi(settings["LENGTH"])
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if idx == -1 {
|
||||||
|
idx = len(tag)
|
||||||
|
}
|
||||||
|
|
||||||
if idx != -1 {
|
if idx != -1 {
|
||||||
name = tag[0:idx]
|
name = tag[0:idx]
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,8 @@ type UserIndex struct {
|
||||||
Name5 int64 `gorm:"index:,class:FULLTEXT,comment:hello \\, world,where:age > 10"`
|
Name5 int64 `gorm:"index:,class:FULLTEXT,comment:hello \\, world,where:age > 10"`
|
||||||
Name6 int64 `gorm:"index:profile,comment:hello \\, world,where:age > 10"`
|
Name6 int64 `gorm:"index:profile,comment:hello \\, world,where:age > 10"`
|
||||||
Age int64 `gorm:"index:profile,expression:ABS(age)"`
|
Age int64 `gorm:"index:profile,expression:ABS(age)"`
|
||||||
|
OID int64 `gorm:"index:idx_id"`
|
||||||
|
MemberNumber string `gorm:"index:idx_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseIndex(t *testing.T) {
|
func TestParseIndex(t *testing.T) {
|
||||||
|
@ -64,6 +66,10 @@ func TestParseIndex(t *testing.T) {
|
||||||
Expression: "ABS(age)",
|
Expression: "ABS(age)",
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
|
"idx_id": {
|
||||||
|
Name: "idx_id",
|
||||||
|
Fields: []schema.IndexOption{{}, {}},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
indices := user.ParseIndexes()
|
indices := user.ParseIndexes()
|
||||||
|
@ -71,7 +77,7 @@ func TestParseIndex(t *testing.T) {
|
||||||
for k, result := range results {
|
for k, result := range results {
|
||||||
v, ok := indices[k]
|
v, ok := indices[k]
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Errorf("Failed to found index %v from parsed indices %+v", k, indices)
|
t.Fatalf("Failed to found index %v from parsed indices %+v", k, indices)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, name := range []string{"Name", "Class", "Type", "Where", "Comment"} {
|
for _, name := range []string{"Name", "Class", "Type", "Where", "Comment"} {
|
||||||
|
|
Loading…
Reference in New Issue