From 39c8d6220b75b5a28dfff6ae88da17485b35dc46 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 6 Aug 2020 17:48:46 +0800 Subject: [PATCH] Fix soft delete panic when using unaddressable value --- soft_delete.go | 2 +- tests/delete_test.go | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/soft_delete.go b/soft_delete.go index 6b88b1a5..180bf745 100644 --- a/soft_delete.go +++ b/soft_delete.go @@ -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) diff --git a/tests/delete_test.go b/tests/delete_test.go index 3d461f65..f5b3e784 100644 --- a/tests/delete_test.go +++ b/tests/delete_test.go @@ -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) {