From e483cd9993199dafb4e3ceb9065a81a2dc039144 Mon Sep 17 00:00:00 2001 From: Dave Shrewsberry Date: Thu, 1 Aug 2024 16:31:37 -0400 Subject: [PATCH] put check back in drop --- migrator/migrator.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/migrator/migrator.go b/migrator/migrator.go index c763c8a9..7d923851 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -587,7 +587,7 @@ func (m Migrator) MigrateColumnUnique(value interface{}, field *schema.Field, co // We're currently only receiving boolean values on `Unique` tag, // so the UniqueConstraint name is fixed constraint := m.DB.NamingStrategy.UniqueName(stmt.Table, field.DBName) - if unique && !field.Unique && m.HasConstraint(value, constraint) { + if unique && !field.Unique { return m.DB.Migrator().DropConstraint(value, constraint) } if !unique && field.Unique { @@ -758,11 +758,13 @@ func (m Migrator) CreateConstraint(value interface{}, name string) error { // DropConstraint drop constraint func (m Migrator) DropConstraint(value interface{}, name string) error { return m.RunWithValue(value, func(stmt *gorm.Statement) error { + if !m.HasConstraint(value, name) { + return nil + } constraint, table := m.GuessConstraintInterfaceAndTable(stmt, name) if constraint != nil { name = constraint.GetName() } - return m.DB.Exec("ALTER TABLE ? DROP CONSTRAINT ?", clause.Table{Name: table}, clause.Column{Name: name}).Error }) }