Skip to content

Commit 8bc1d4f

Browse files
authored
fix: ignore add constraint on unique index (#157)
1 parent d857acc commit 8bc1d4f

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

migrator.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,11 @@ func (m Migrator) AlterColumn(value interface{}, field string) error {
336336

337337
if uniq, _ := fieldColumnType.Unique(); !uniq && field.Unique {
338338
idxName := clause.Column{Name: m.DB.Config.NamingStrategy.IndexName(stmt.Table, field.DBName)}
339-
if err := m.DB.Exec("ALTER TABLE ? ADD CONSTRAINT ? UNIQUE(?)", m.CurrentTable(stmt), idxName, clause.Column{Name: field.DBName}).Error; err != nil {
340-
return err
339+
// Not a unique constraint but a unique index
340+
if !m.HasIndex(stmt.Table, idxName.Name) {
341+
if err := m.DB.Exec("ALTER TABLE ? ADD CONSTRAINT ? UNIQUE(?)", m.CurrentTable(stmt), idxName, clause.Column{Name: field.DBName}).Error; err != nil {
342+
return err
343+
}
341344
}
342345
}
343346

0 commit comments

Comments
 (0)