put check back in drop

This commit is contained in:
Dave Shrewsberry 2024-08-01 16:31:37 -04:00
parent 79e73f0d7d
commit e483cd9993
1 changed files with 4 additions and 2 deletions

View File

@ -587,7 +587,7 @@ func (m Migrator) MigrateColumnUnique(value interface{}, field *schema.Field, co
// We're currently only receiving boolean values on `Unique` tag, // We're currently only receiving boolean values on `Unique` tag,
// so the UniqueConstraint name is fixed // so the UniqueConstraint name is fixed
constraint := m.DB.NamingStrategy.UniqueName(stmt.Table, field.DBName) 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) return m.DB.Migrator().DropConstraint(value, constraint)
} }
if !unique && field.Unique { if !unique && field.Unique {
@ -758,11 +758,13 @@ func (m Migrator) CreateConstraint(value interface{}, name string) error {
// DropConstraint drop constraint // DropConstraint drop constraint
func (m Migrator) DropConstraint(value interface{}, name string) error { func (m Migrator) DropConstraint(value interface{}, name string) error {
return m.RunWithValue(value, func(stmt *gorm.Statement) error { return m.RunWithValue(value, func(stmt *gorm.Statement) error {
if !m.HasConstraint(value, name) {
return nil
}
constraint, table := m.GuessConstraintInterfaceAndTable(stmt, name) constraint, table := m.GuessConstraintInterfaceAndTable(stmt, name)
if constraint != nil { if constraint != nil {
name = constraint.GetName() name = constraint.GetName()
} }
return m.DB.Exec("ALTER TABLE ? DROP CONSTRAINT ?", clause.Table{Name: table}, clause.Column{Name: name}).Error return m.DB.Exec("ALTER TABLE ? DROP CONSTRAINT ?", clause.Table{Name: table}, clause.Column{Name: name}).Error
}) })
} }