Skip to content

Commit 6b45756

Browse files
committed
Fix update blank value with Select
1 parent 5e9c0f5 commit 6b45756

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

Diff for: go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/stretchr/testify v1.7.0
77
gorm.io/driver/postgres v1.3.1
88
gorm.io/driver/sqlite v1.2.6
9-
gorm.io/gorm v1.23.2
9+
gorm.io/gorm v1.23.8
1010
)
1111

1212
require (
@@ -20,7 +20,7 @@ require (
2020
github.com/jackc/pgtype v1.9.1 // indirect
2121
github.com/jackc/pgx/v4 v4.14.1 // indirect
2222
github.com/jinzhu/inflection v1.0.0 // indirect
23-
github.com/jinzhu/now v1.1.4 // indirect
23+
github.com/jinzhu/now v1.1.5 // indirect
2424
github.com/mattn/go-sqlite3 v1.14.9 // indirect
2525
github.com/pmezard/go-difflib v1.0.0 // indirect
2626
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect

Diff for: go.sum

+4-3
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ github.com/jackc/puddle v1.2.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dv
6767
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
6868
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
6969
github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
70-
github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
7170
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
71+
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
72+
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
7273
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
7374
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
7475
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
@@ -193,6 +194,6 @@ gorm.io/driver/sqlite v1.2.6 h1:SStaH/b+280M7C8vXeZLz/zo9cLQmIGwwj3cSj7p6l4=
193194
gorm.io/driver/sqlite v1.2.6/go.mod h1:gyoX0vHiiwi0g49tv+x2E7l8ksauLK0U/gShcdUsjWY=
194195
gorm.io/gorm v1.22.3/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
195196
gorm.io/gorm v1.23.1/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
196-
gorm.io/gorm v1.23.2 h1:xmq9QRMWL8HTJyhAUBXy8FqIIQCYESeKfJL4DoGKiWQ=
197-
gorm.io/gorm v1.23.2/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
197+
gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE=
198+
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
198199
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=

Diff for: version.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,20 @@ func (v VersionUpdateClause) ModifyStatement(stmt *gorm.Statement) {
131131
// struct to map
132132
dv := reflect.ValueOf(stmt.Dest)
133133
if reflect.Indirect(dv).Kind() == reflect.Struct {
134+
selectColumns, restricted := stmt.SelectAndOmitColumns(false, true)
135+
134136
sd, _ := schema.Parse(stmt.Dest, &sync.Map{}, stmt.DB.NamingStrategy)
135137
d := make(map[string]interface{})
136138
for _, field := range sd.Fields {
137139
if field.DBName == v.Field.DBName {
138140
continue
139141
}
140142

141-
if val, zero := field.ValueOf(stmt.Context, dv); !zero {
142-
d[field.DBName] = val
143+
if v, ok := selectColumns[field.DBName]; (ok && v) || (!ok && (!restricted || (!stmt.SkipHooks && field.AutoUpdateTime > 0))) {
144+
val, isZero := field.ValueOf(stmt.Context, dv)
145+
if (ok || !isZero) && field.Updatable {
146+
d[field.DBName] = val
147+
}
143148
}
144149
}
145150

Diff for: version_test.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ import (
1212
"gorm.io/gorm"
1313
)
1414

15+
var (
16+
mysqlDSN = "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local"
17+
postgresDSN = "user=gorm password=gorm dbname=gorm host=localhost port=9920 sslmode=disable TimeZone=Asia/Shanghai"
18+
sqlserverDSN = "sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm"
19+
)
20+
1521
type User struct {
1622
ID int
1723
Name string
@@ -21,7 +27,6 @@ type User struct {
2127

2228
func TestVersion(t *testing.T) {
2329
DB, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
24-
DB = DB.Debug()
2530
require.Nil(t, err)
2631

2732
user := User{Name: "bob", Age: 20}
@@ -52,11 +57,12 @@ func TestVersion(t *testing.T) {
5257
require.Equal(t, int64(4), user.Version.Int64)
5358
require.Equal(t, uint(12), user.Age)
5459

55-
rows = DB.Model(&user).Updates(&User{Name: "lewis"}).RowsAffected
60+
rows = DB.Model(&user).Select("name", "age", "version").Updates(&User{Name: "lewis"}).RowsAffected
5661
require.Equal(t, int64(1), rows)
5762
require.Nil(t, DB.First(&user).Error)
5863
require.Equal(t, int64(5), user.Version.Int64)
5964
require.Equal(t, "lewis", user.Name)
65+
require.Equal(t, uint(0), user.Age)
6066

6167
rows = DB.Model(&user).Updates(map[string]interface{}{
6268
"age": 18,
@@ -145,7 +151,6 @@ func (e Ext) Value() (driver.Value, error) {
145151

146152
func TestEmbed(t *testing.T) {
147153
DB, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
148-
DB = DB.Debug()
149154
require.Nil(t, err)
150155

151156
user := User{Name: "bob", Age: 20}
@@ -189,11 +194,9 @@ func TestEmbed(t *testing.T) {
189194
require.Equal(t, int64(3), a1.Version.Int64)
190195
}
191196

192-
// docker run --name gormpostgresql -e POSTGRES_PASSWORD=gorm -e POSTGRES_USER=gorm -d -p 5432:5432 postgres
197+
// use gorm.io/gorm/tests docker compose file
193198
func TestPostgres(t *testing.T) {
194-
dsn := "host=127.0.0.1 user=gorm password=gorm dbname=gorm port=5432 sslmode=disable TimeZone=Asia/Shanghai"
195-
DB, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
196-
DB = DB.Debug()
199+
DB, err := gorm.Open(postgres.Open(postgresDSN), &gorm.Config{})
197200
require.Nil(t, err)
198201

199202
user := User{Name: "bob", Age: 20}

0 commit comments

Comments
 (0)