diff --git a/callbacks/create.go b/callbacks/create.go index 18f25c9a..ac63c89b 100644 --- a/callbacks/create.go +++ b/callbacks/create.go @@ -153,6 +153,7 @@ func CreateWithReturning(db *gorm.DB) { } if rows.Next() { + db.RowsAffected++ err = rows.Scan(values...) } } diff --git a/dialects/mysql/mysql.go b/dialects/mysql/mysql.go index 23525ed7..baeb79c7 100644 --- a/dialects/mysql/mysql.go +++ b/dialects/mysql/mysql.go @@ -60,6 +60,13 @@ func (dialector Dialector) ClauseBuilders() map[string]clause.ClauseBuilder { c.Build(builder) } }, + "VALUES": func(c clause.Clause, builder clause.Builder) { + if values, ok := c.Expression.(clause.Values); ok && len(values.Columns) == 0 { + builder.WriteString("VALUES()") + return + } + c.Build(builder) + }, } } diff --git a/tests/create_test.go b/tests/create_test.go index 5b859e99..43e2c718 100644 --- a/tests/create_test.go +++ b/tests/create_test.go @@ -9,8 +9,10 @@ import ( func TestCreate(t *testing.T) { var user = *GetUser("create", Config{}) - if err := DB.Create(&user).Error; err != nil { - t.Fatalf("errors happened when create: %v", err) + if results := DB.Create(&user); results.Error != nil { + t.Fatalf("errors happened when create: %v", results.Error) + } else if results.RowsAffected != 1 { + t.Fatalf("rows affected expects: %v, got %v", 1, results.RowsAffected) } if user.ID == 0 { @@ -68,8 +70,10 @@ func TestBulkCreateWithAssociations(t *testing.T) { *GetUser("bulk_8", Config{Account: false, Pets: 0, Toys: 0, Company: false, Manager: false, Team: 0, Languages: 0, Friends: 0}), } - if err := DB.Create(&users).Error; err != nil { - t.Fatalf("errors happened when create: %v", err) + if results := DB.Create(&users); results.Error != nil { + t.Fatalf("errors happened when create: %v", results.Error) + } else if results.RowsAffected != int64(len(users)) { + t.Fatalf("rows affected expects: %v, got %v", len(users), results.RowsAffected) } var userIDs []uint @@ -182,3 +186,18 @@ func TestPolymorphicHasOne(t *testing.T) { } }) } + +func TestCreateEmptyStrut(t *testing.T) { + type EmptyStruct struct { + ID uint + } + DB.Migrator().DropTable(&EmptyStruct{}) + + if err := DB.AutoMigrate(&EmptyStruct{}); err != nil { + t.Errorf("no error should happen when auto migrate, but got %v", err) + } + + if err := DB.Create(&EmptyStruct{}).Error; err != nil { + t.Errorf("No error should happen when creating user, but got %v", err) + } +}