Skip to content

Commit

Permalink
fix: quote index when creating table (#5331)
Browse files Browse the repository at this point in the history
  • Loading branch information
black-06 committed May 17, 2022
1 parent 373bcf7 commit f5e77aa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion migrator/migrator.go
Expand Up @@ -223,7 +223,7 @@ func (m Migrator) CreateTable(values ...interface{}) error {
}

createTableSQL += ","
values = append(values, clause.Expr{SQL: idx.Name}, tx.Migrator().(BuildIndexOptionsInterface).BuildIndexOptions(idx.Fields, stmt))
values = append(values, clause.Column{Name: idx.Name}, tx.Migrator().(BuildIndexOptionsInterface).BuildIndexOptions(idx.Fields, stmt))
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/migrate_test.go
Expand Up @@ -262,6 +262,25 @@ func TestMigrateTable(t *testing.T) {
}
}

func TestMigrateWithQuotedIndex(t *testing.T) {
if DB.Dialector.Name() != "mysql" {
t.Skip()
}

type QuotedIndexStruct struct {
gorm.Model
Name string `gorm:"size:255;index:AS"` // AS is one of MySQL reserved words
}

if err := DB.Migrator().DropTable(&QuotedIndexStruct{}); err != nil {
t.Fatalf("Failed to drop table, got error %v", err)
}

if err := DB.AutoMigrate(&QuotedIndexStruct{}); err != nil {
t.Fatalf("Failed to auto migrate, but got error %v", err)
}
}

func TestMigrateIndexes(t *testing.T) {
type IndexStruct struct {
gorm.Model
Expand Down

0 comments on commit f5e77aa

Please sign in to comment.