Fix soft delete panic when using unaddressable value

This commit is contained in:
Jinzhu 2020-08-06 17:48:46 +08:00
parent 3df249c127
commit 39c8d6220b
2 changed files with 9 additions and 1 deletions

View File

@ -64,7 +64,7 @@ func (SoftDeleteClause) ModifyStatement(stmt *Statement) {
stmt.AddClause(clause.Where{Exprs: []clause.Expression{clause.IN{Column: column, Values: values}}})
}
if stmt.Dest != stmt.Model && stmt.Model != nil {
if stmt.ReflectValue.CanAddr() && stmt.Dest != stmt.Model && stmt.Model != nil {
_, queryValues = schema.GetIdentityFieldValuesMap(reflect.ValueOf(stmt.Model), stmt.Schema.PrimaryFields)
column, values = schema.ToQueryValues(stmt.Table, stmt.Schema.PrimaryFieldDBNames, queryValues)

View File

@ -43,6 +43,14 @@ func TestDelete(t *testing.T) {
t.Errorf("no error should returns when query %v, but got %v", user.ID, err)
}
}
if err := DB.Delete(users[0]).Error; err != nil {
t.Errorf("errors happened when delete: %v", err)
}
if err := DB.Where("id = ?", users[0].ID).First(&result).Error; err == nil || !errors.Is(err, gorm.ErrRecordNotFound) {
t.Errorf("should returns record not found error, but got %v", err)
}
}
func TestDeleteWithTable(t *testing.T) {