From 4b224e882327a1d331c30c90d5c29d6a68dae69d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=96=87=E5=BC=BA?= Date: Thu, 26 Jul 2018 20:05:25 +0800 Subject: [PATCH] Fix genUpdateColumns() should not ignore soft delete column when unscoped (#1051) --- session_update.go | 2 +- session_update_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/session_update.go b/session_update.go index 84c7e7fec..42dfaacd0 100644 --- a/session_update.go +++ b/session_update.go @@ -443,7 +443,7 @@ func (session *Session) genUpdateColumns(bean interface{}) ([]string, []interfac } } - if col.IsDeleted || col.IsCreated { + if (col.IsDeleted && !session.statement.unscoped) || col.IsCreated { continue } diff --git a/session_update_test.go b/session_update_test.go index 8d5b45877..2a7005ee0 100644 --- a/session_update_test.go +++ b/session_update_test.go @@ -1133,6 +1133,54 @@ func TestCreatedUpdated2(t *testing.T) { assert.True(t, s2.UpdateAt.Unix() > s2.CreateAt.Unix()) } +func TestDeletedUpdate(t *testing.T) { + assert.NoError(t, prepareEngine()) + + type DeletedUpdatedStruct struct { + Id int64 + Name string + DeletedAt time.Time `xorm:"deleted"` + } + + assertSync(t, new(DeletedUpdatedStruct)) + + var s = DeletedUpdatedStruct{ + Name: "test", + } + cnt, err := testEngine.Insert(&s) + assert.NoError(t, err) + assert.EqualValues(t, 1, cnt) + + cnt, err = testEngine.ID(s.Id).Delete(&DeletedUpdatedStruct{}) + assert.NoError(t, err) + assert.EqualValues(t, 1, cnt) + + s.DeletedAt = time.Time{} + cnt, err = testEngine.Unscoped().Nullable("deleted_at").Update(&s) + assert.NoError(t, err) + assert.EqualValues(t, 1, cnt) + + var s1 DeletedUpdatedStruct + has, err := testEngine.ID(s.Id).Get(&s1) + assert.EqualValues(t, true, has) + + cnt, err = testEngine.ID(s.Id).Delete(&DeletedUpdatedStruct{}) + assert.NoError(t, err) + assert.EqualValues(t, 1, cnt) + + cnt, err = testEngine.ID(s.Id).Cols("deleted_at").Update(&DeletedUpdatedStruct{}) + assert.EqualValues(t, "No content found to be updated", err.Error()) + assert.EqualValues(t, 0, cnt) + + cnt, err = testEngine.ID(s.Id).Unscoped().Cols("deleted_at").Update(&DeletedUpdatedStruct{}) + assert.NoError(t, err) + assert.EqualValues(t, 1, cnt) + + var s2 DeletedUpdatedStruct + has, err = testEngine.ID(s.Id).Get(&s2) + assert.EqualValues(t, true, has) +} + func TestUpdateMapCondition(t *testing.T) { assert.NoError(t, prepareEngine())