Fix #929 #1936

Merged
lunny merged 7 commits from lunny/insert_deleted into master 2021-07-03 12:26:50 +00:00
2 changed files with 31 additions and 0 deletions
Showing only changes of commit 761926fd8b - Show all commits

View File

@ -1024,3 +1024,27 @@ func TestInsertIntSlice(t *testing.T) {
assert.True(t, has)
assert.EqualValues(t, v3, v4)
}
func TestInsertDeleted(t *testing.T) {
assert.NoError(t, PrepareEngine())
type InsertDeletedStruct struct {
ID uint64 `xorm:"'ID' pk autoincr"`
DeletedAt time.Time `xorm:"'DELETED_AT' deleted notnull"`
}
assert.NoError(t, testEngine.Sync2(new(InsertDeletedStruct)))
_, err := testEngine.Insert(&InsertDeletedStruct{})
assert.NoError(t, err)
var v InsertDeletedStruct
has, err := testEngine.Get(&v)
assert.NoError(t, err)
assert.False(t, has)
var v2 InsertDeletedStruct
has, err = testEngine.Unscoped().Get(&v2)
assert.NoError(t, err)
assert.True(t, has)
}

View File

@ -11,6 +11,7 @@ import (
"sort"
"strconv"
"strings"
"time"
"xorm.io/xorm/internal/utils"
"xorm.io/xorm/schemas"
@ -497,6 +498,12 @@ func (session *Session) genInsertColumns(bean interface{}) ([]string, []interfac
}
if col.IsDeleted {
colNames = append(colNames, col.Name)
if !col.Nullable {
args = append(args, time.Time{})
} else {
args = append(args, nil)
}
continue
}