Fix RowsAffected not returned for FirstOrCreate
This commit is contained in:
parent
a667ab8427
commit
89b7cbe89c
6
main.go
6
main.go
@ -330,13 +330,13 @@ func (s *DB) FirstOrInit(out interface{}, where ...interface{}) *DB {
|
|||||||
// https://jinzhu.github.io/gorm/curd.html#firstorcreate
|
// https://jinzhu.github.io/gorm/curd.html#firstorcreate
|
||||||
func (s *DB) FirstOrCreate(out interface{}, where ...interface{}) *DB {
|
func (s *DB) FirstOrCreate(out interface{}, where ...interface{}) *DB {
|
||||||
c := s.clone()
|
c := s.clone()
|
||||||
if result := c.First(out, where...); result.Error != nil {
|
if result := s.First(out, where...); result.Error != nil {
|
||||||
if !result.RecordNotFound() {
|
if !result.RecordNotFound() {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
c.AddError(c.NewScope(out).inlineCondition(where...).initialize().callCallbacks(c.parent.callbacks.creates).db.Error)
|
return c.NewScope(out).inlineCondition(where...).initialize().callCallbacks(c.parent.callbacks.creates).db
|
||||||
} else if len(c.search.assignAttrs) > 0 {
|
} else if len(c.search.assignAttrs) > 0 {
|
||||||
c.AddError(c.NewScope(out).InstanceSet("gorm:update_interface", c.search.assignAttrs).callCallbacks(c.parent.callbacks.updates).db.Error)
|
return c.NewScope(out).InstanceSet("gorm:update_interface", c.search.assignAttrs).callCallbacks(c.parent.callbacks.updates).db
|
||||||
}
|
}
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
10
main_test.go
10
main_test.go
@ -315,8 +315,14 @@ func TestNullValuesWithFirstOrCreate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var nv2 NullValue
|
var nv2 NullValue
|
||||||
if err := DB.Where(nv1).FirstOrCreate(&nv2).Error; err != nil {
|
result := DB.Where(nv1).FirstOrCreate(&nv2)
|
||||||
t.Errorf("Should not raise any error, but got %v", err)
|
|
||||||
|
if result.RowsAffected != 1 {
|
||||||
|
t.Errorf("RowsAffected should be 1 after create some record")
|
||||||
|
}
|
||||||
|
|
||||||
|
if result.Error != nil {
|
||||||
|
t.Errorf("Should not raise any error, but got %v", result.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
if nv2.Name.String != "first_or_create" || nv2.Gender.String != "M" {
|
if nv2.Name.String != "first_or_create" || nv2.Gender.String != "M" {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user