fix: quote index when creating table

This commit is contained in:
小黑 2022-05-12 12:47:26 +08:00
parent 373bcf7aca
commit 4727b536ca
2 changed files with 20 additions and 1 deletions

View File

@ -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))
}
}

View File

@ -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