This commit is contained in:
Dave Shrewsberry 2024-08-01 17:01:58 -04:00
parent e483cd9993
commit 38fb45fea8
2 changed files with 6 additions and 1 deletions

View File

@ -759,7 +759,8 @@ func (m Migrator) CreateConstraint(value interface{}, name string) error {
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) { 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) constraint, table := m.GuessConstraintInterfaceAndTable(stmt, name)
if constraint != nil { if constraint != nil {

View File

@ -681,6 +681,10 @@ func TestMigrateConstraint(t *testing.T) {
DB.Migrator().CreateConstraint(&User{}, name) 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 { if err := DB.Migrator().DropConstraint(&User{}, name); err != nil {
t.Fatalf("failed to drop constraint %v, got error %v", name, err) t.Fatalf("failed to drop constraint %v, got error %v", name, err)
} }