diff --git a/migrator/migrator.go b/migrator/migrator.go index d49894108..757ab9494 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -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)) } } diff --git a/tests/migrate_test.go b/tests/migrate_test.go index f862eda0f..12eb8ed05 100644 --- a/tests/migrate_test.go +++ b/tests/migrate_test.go @@ -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