mirror of https://github.com/go-gorm/gorm.git
Merge 003bcc3a35
into 6bfccf8afa
This commit is contained in:
commit
fe77499e59
|
@ -710,6 +710,7 @@ func (m Migrator) GuessConstraintInterfaceAndTable(stmt *gorm.Statement, name st
|
|||
|
||||
for _, rel := range stmt.Schema.Relationships.Relations {
|
||||
if constraint := rel.ParseConstraint(); constraint != nil && constraint.Name == name {
|
||||
fmt.Println(fmt.Sprintf("SHREWS --Out of Guess for relation 0 %v", constraint))
|
||||
return constraint, getTable(rel)
|
||||
}
|
||||
}
|
||||
|
@ -718,6 +719,7 @@ func (m Migrator) GuessConstraintInterfaceAndTable(stmt *gorm.Statement, name st
|
|||
for k := range checkConstraints {
|
||||
if checkConstraints[k].Field == field {
|
||||
v := checkConstraints[k]
|
||||
fmt.Println(fmt.Sprintf("SHREWS --Out of Guess for check %v", &v))
|
||||
return &v, stmt.Table
|
||||
}
|
||||
}
|
||||
|
@ -725,12 +727,14 @@ func (m Migrator) GuessConstraintInterfaceAndTable(stmt *gorm.Statement, name st
|
|||
for k := range uniqueConstraints {
|
||||
if uniqueConstraints[k].Field == field {
|
||||
v := uniqueConstraints[k]
|
||||
fmt.Println(fmt.Sprintf("SHREWS --Out of Guess for unique %v", &v))
|
||||
return &v, stmt.Table
|
||||
}
|
||||
}
|
||||
|
||||
for _, rel := range stmt.Schema.Relationships.Relations {
|
||||
if constraint := rel.ParseConstraint(); constraint != nil && rel.Field == field {
|
||||
fmt.Println(fmt.Sprintf("SHREWS --Out of Guess for relation %v", constraint))
|
||||
return constraint, getTable(rel)
|
||||
}
|
||||
}
|
||||
|
@ -757,7 +761,11 @@ func (m Migrator) CreateConstraint(value interface{}, name string) error {
|
|||
|
||||
// DropConstraint drop constraint
|
||||
func (m Migrator) DropConstraint(value interface{}, name string) error {
|
||||
fmt.Println(fmt.Sprintf("SHREWS --In Drop %q", name))
|
||||
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
|
||||
if !m.HasConstraint(value, name) {
|
||||
return nil
|
||||
}
|
||||
constraint, table := m.GuessConstraintInterfaceAndTable(stmt, name)
|
||||
if constraint != nil {
|
||||
name = constraint.GetName()
|
||||
|
|
|
@ -723,6 +723,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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue