From fe01e1b9f43070e3814817b4b762dfd08a3ced30 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 7 Jul 2022 14:43:33 +0800 Subject: [PATCH] Fix Model with slice data --- callbacks/update.go | 2 +- tests/go.mod | 12 +++++++----- tests/update_test.go | 8 ++++++++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/callbacks/update.go b/callbacks/update.go index 01f40509..42ffe2f6 100644 --- a/callbacks/update.go +++ b/callbacks/update.go @@ -172,7 +172,7 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) { } } - stmt.AddClause(clause.Where{Exprs: []clause.Expression{clause.Or(primaryKeyExprs...)}}) + stmt.AddClause(clause.Where{Exprs: []clause.Expression{clause.And(clause.Or(primaryKeyExprs...))}}) } case reflect.Struct: for _, field := range stmt.Schema.PrimaryFields { diff --git a/tests/go.mod b/tests/go.mod index bd668420..f3e9d260 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -3,16 +3,18 @@ module gorm.io/gorm/tests go 1.14 require ( + github.com/denisenkom/go-mssqldb v0.12.2 // indirect github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect github.com/google/uuid v1.3.0 github.com/jinzhu/now v1.1.5 - github.com/lib/pq v1.10.5 - golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect + github.com/lib/pq v1.10.6 + github.com/mattn/go-sqlite3 v1.14.14 // indirect + golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect gorm.io/driver/mysql v1.3.4 - gorm.io/driver/postgres v1.3.5 - gorm.io/driver/sqlite v1.3.2 + gorm.io/driver/postgres v1.3.8 + gorm.io/driver/sqlite v1.3.6 gorm.io/driver/sqlserver v1.3.2 - gorm.io/gorm v1.23.4 + gorm.io/gorm v1.23.7 ) replace gorm.io/gorm => ../ diff --git a/tests/update_test.go b/tests/update_test.go index 41ea5d27..0fc89a93 100644 --- a/tests/update_test.go +++ b/tests/update_test.go @@ -122,6 +122,14 @@ func TestUpdate(t *testing.T) { } else { CheckUser(t, result4, *user) } + + if rowsAffected := DB.Model([]User{result4}).Where("age > 0").Update("name", "jinzhu").RowsAffected; rowsAffected != 1 { + t.Errorf("should only update one record, but got %v", rowsAffected) + } + + if rowsAffected := DB.Model(users).Where("age > 0").Update("name", "jinzhu").RowsAffected; rowsAffected != 3 { + t.Errorf("should only update one record, but got %v", rowsAffected) + } } func TestUpdates(t *testing.T) {