Skip to content

Commit

Permalink
fix: ignore add constraint on unique index (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
a631807682 authored Feb 8, 2023
1 parent d857acc commit 8bc1d4f
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,11 @@ func (m Migrator) AlterColumn(value interface{}, field string) error {

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

Expand Down

0 comments on commit 8bc1d4f

Please sign in to comment.