Skip to content

Commit

Permalink
Merge pull request #28
Browse files Browse the repository at this point in the history
add save() test case and update readme
  • Loading branch information
icpd authored Nov 10, 2023
2 parents 7244ed9 + 5a3696e commit 6aee054
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ DB.First(&user)
DB.Model(&user).Update("age", 18)
// UPDATE `users` SET `age`=18,`version`=`version`+1 WHERE `users`.`version` = 1 AND `id` = 1

// Ignoring the optimistic lock check.
DB.Unscoped().Model(&user).Update("age", 18)
// UPDATE `users` SET `age`=18,`version`=`version`+1 WHERE `id` = 1

// The Model's Version value passed in to Updates will be ignored
// Ignoring the passed Version value.
DB.Model(&user).Updates(&User{Age: 18, Version: optimisticlock.Version{Int64: 1}})
// UPDATE `users` SET `age`=18,`version`=`version`+1 WHERE `users`.`version` = 3 AND `id` = 1

// Model's Version is zero, then no condition will be added to the WHERE
// If the Model's Version value is zero, Without considering optimistic lock check.
DB.Model(&User{}).Where("id = 1").Update("age", 12)
// UPDATE `users` SET `age`=12,`version`=`version`+1 WHERE id = 1

// When want to use GORM's Save method, need to call Select. Otherwise, will return primary key duplicate error.
// The Select param is the fields that you want to update or "*".
DB.Model(&user).Select("*").Updates(&User{Age: 18})
```
6 changes: 6 additions & 0 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ func TestVersion(t *testing.T) {
require.Equal(t, date, user.UpdatedAt)
DB.NowFunc = time.Now

// Call Select method. Otherwise, will return primary key duplicate error.
user.Name = "lucky"
tx := DB.Select("*").Save(&user)
require.Nil(t, tx.Error)
require.Equal(t, int64(0), tx.RowsAffected)

// support create
users := []User{{Name: "foo", Age: 30}, {Name: "bar", Age: 40, Version: Version{Int64: 100}}}
DB.Create(&users)
Expand Down

0 comments on commit 6aee054

Please sign in to comment.