From e55b2ba3120b7bdc797d007e73455ab68207c2d4 Mon Sep 17 00:00:00 2001 From: Dave Shrewsberry Date: Thu, 1 Aug 2024 14:32:27 -0400 Subject: [PATCH] if exists doesn't work for mysql, so have to verify constraint exists. --- migrator/migrator.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/migrator/migrator.go b/migrator/migrator.go index 1c15d1ff..d9218ede 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -762,8 +762,11 @@ func (m Migrator) DropConstraint(value interface{}, name string) error { if constraint != nil { name = constraint.GetName() } - // using IF EXISTS here as we are "guessing" the constraint name - return m.DB.Exec("ALTER TABLE ? DROP CONSTRAINT IF EXISTS ?", clause.Table{Name: table}, clause.Column{Name: name}).Error + // ensure the constraint exists first as we are "guessing" the constraint name + if m.HasConstraint(value, name) { + return m.DB.Exec("ALTER TABLE ? DROP CONSTRAINT ?", clause.Table{Name: table}, clause.Column{Name: name}).Error + } + return nil }) }