if exists doesn't work for mysql, so have to verify constraint exists.

This commit is contained in:
Dave Shrewsberry 2024-08-01 14:32:27 -04:00
parent 92bc52270e
commit e55b2ba312

View File

@ -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
})
}