diff --git a/migrator/migrator.go b/migrator/migrator.go index 7d923851..678cf566 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -759,7 +759,8 @@ func (m Migrator) CreateConstraint(value interface{}, name string) error { 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, _ := m.GuessConstraintInterfaceAndTable(stmt, name) + return errors.New(fmt.Sprintf("Can't find constraint for %q, with guessed name %q", name, constraint)) } constraint, table := m.GuessConstraintInterfaceAndTable(stmt, name) if constraint != nil { diff --git a/tests/migrate_test.go b/tests/migrate_test.go index d955c8d7..079dde0f 100644 --- a/tests/migrate_test.go +++ b/tests/migrate_test.go @@ -681,6 +681,10 @@ func TestMigrateConstraint(t *testing.T) { DB.Migrator().CreateConstraint(&User{}, name) } + if !DB.Migrator().HasConstraint(&User{}, name) { + t.Fatalf("failed to create constraint %v", name) + } + if err := DB.Migrator().DropConstraint(&User{}, name); err != nil { t.Fatalf("failed to drop constraint %v, got error %v", name, err) }