diff --git a/migrator/migrator.go b/migrator/migrator.go index a239c926..c8fe17ab 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -175,6 +175,9 @@ func (m Migrator) CreateTable(values ...interface{}) error { errr = tx.Migrator().CreateIndex(value, name) }(value, idx.Name) } else { + if idx.Class != "" { + createTableSQL += idx.Class + " " + } createTableSQL += "INDEX ? ?," values = append(values, clause.Expr{SQL: idx.Name}, tx.Migrator().(BuildIndexOptionsInterface).BuildIndexOptions(idx.Fields, stmt)) } diff --git a/tests/migrate_test.go b/tests/migrate_test.go index fce4c4aa..2c593a70 100644 --- a/tests/migrate_test.go +++ b/tests/migrate_test.go @@ -62,6 +62,23 @@ func TestMigrateWithComment(t *testing.T) { } } +func TestMigrateWithUniqueIndex(t *testing.T) { + type UserWithUniqueIndex struct { + ID int + Name string `gorm:"size:20;index:idx_name,unique"` + Date time.Time `gorm:"index:idx_name,unique"` + } + + DB.Migrator().DropTable(&UserWithUniqueIndex{}) + if err := DB.AutoMigrate(&UserWithUniqueIndex{}); err != nil { + t.Fatalf("failed to migrate, got %v", err) + } + + if !DB.Migrator().HasIndex(&UserWithUniqueIndex{}, "idx_name") { + t.Errorf("Failed to find created index") + } +} + func TestTable(t *testing.T) { type TableStruct struct { gorm.Model