fix: Save not use soft_delete

This commit is contained in:
kinggo 2021-12-03 17:27:44 +08:00
parent 300a23fc31
commit e78c92378b
2 changed files with 14 additions and 1 deletions

View File

@ -73,6 +73,13 @@ func Update(config *Config) func(db *gorm.DB) {
} else if _, ok := db.Statement.Clauses["SET"]; !ok { } else if _, ok := db.Statement.Clauses["SET"]; !ok {
return return
} }
if _, ok := db.Statement.Clauses["WHERE"]; ok && db.Statement.Schema != nil && !db.Statement.Unscoped {
for _, c := range db.Statement.Schema.UpdateClauses {
db.Statement.AddClause(c)
}
}
db.Statement.Build(db.Statement.BuildClauses...) db.Statement.Build(db.Statement.BuildClauses...)
} }

View File

@ -645,7 +645,13 @@ func TestSave(t *testing.T) {
dryDB := DB.Session(&gorm.Session{DryRun: true}) dryDB := DB.Session(&gorm.Session{DryRun: true})
stmt := dryDB.Save(&user).Statement stmt := dryDB.Save(&user).Statement
if !regexp.MustCompile("WHERE .id. = [^ ]+$").MatchString(stmt.SQL.String()) { if !regexp.MustCompile(`.id. = .* AND .users.\..deleted_at. IS NULL`).MatchString(stmt.SQL.String()) {
t.Fatalf("invalid updating SQL, got %v", stmt.SQL.String())
}
dryDB = DB.Session(&gorm.Session{DryRun: true})
stmt = dryDB.Unscoped().Save(&user).Statement
if !regexp.MustCompile(`WHERE .id. = [^ ]+$`).MatchString(stmt.SQL.String()) {
t.Fatalf("invalid updating SQL, got %v", stmt.SQL.String()) t.Fatalf("invalid updating SQL, got %v", stmt.SQL.String())
} }