mirror of https://github.com/go-gorm/gorm.git
returns ErrEmptySlice when creating with zero length slice
This commit is contained in:
parent
fcb666cfa3
commit
48b395b760
|
@ -252,6 +252,11 @@ func ConvertToCreateValues(stmt *gorm.Statement) (values clause.Values) {
|
|||
stmt.SQL.Grow(stmt.ReflectValue.Len() * 15)
|
||||
values.Values = make([][]interface{}, stmt.ReflectValue.Len())
|
||||
defaultValueFieldsHavingValue := map[*schema.Field][]interface{}{}
|
||||
if stmt.ReflectValue.Len() == 0 {
|
||||
stmt.AddError(gorm.ErrEmptySlice)
|
||||
return
|
||||
}
|
||||
|
||||
for i := 0; i < stmt.ReflectValue.Len(); i++ {
|
||||
rv := reflect.Indirect(stmt.ReflectValue.Index(i))
|
||||
values.Values[i] = make([]interface{}, len(values.Columns))
|
||||
|
|
|
@ -46,6 +46,11 @@ func ConvertSliceOfMapToValuesForCreate(stmt *gorm.Statement, mapValues []map[st
|
|||
selectColumns, restricted = stmt.SelectAndOmitColumns(true, false)
|
||||
)
|
||||
|
||||
if len(mapValues) == 0 {
|
||||
stmt.AddError(gorm.ErrEmptySlice)
|
||||
return
|
||||
}
|
||||
|
||||
for idx, mapValue := range mapValues {
|
||||
for k, v := range mapValue {
|
||||
if stmt.Schema != nil {
|
||||
|
|
|
@ -27,4 +27,6 @@ var (
|
|||
ErrRegistered = errors.New("registered")
|
||||
// ErrInvalidField invalid field
|
||||
ErrInvalidField = errors.New("invalid field")
|
||||
// ErrEmptySlice empty slice found
|
||||
ErrEmptySlice = errors.New("empty slice found")
|
||||
)
|
||||
|
|
|
@ -287,6 +287,18 @@ func TestCreateEmptyStruct(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestCreateEmptySlice(t *testing.T) {
|
||||
var data = []User{}
|
||||
if err := DB.Create(&data).Error; err != gorm.ErrEmptySlice {
|
||||
t.Errorf("no data should be created, got %v", err)
|
||||
}
|
||||
|
||||
var sliceMap = []map[string]interface{}{}
|
||||
if err := DB.Model(&User{}).Create(&sliceMap).Error; err != gorm.ErrEmptySlice {
|
||||
t.Errorf("no data should be created, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateWithExistingTimestamp(t *testing.T) {
|
||||
user := User{Name: "CreateUserExistingTimestamp"}
|
||||
curTime := now.MustParse("2016-01-01")
|
||||
|
|
|
@ -16,3 +16,5 @@ require (
|
|||
replace gorm.io/gorm => ../
|
||||
|
||||
replace github.com/jackc/pgx/v4 => github.com/jinzhu/pgx/v4 v4.8.2
|
||||
|
||||
replace gorm.io/driver/sqlserver => /Users/jinzhu/Projects/jinzhu/sqlserver
|
||||
|
|
Loading…
Reference in New Issue