forked from mirror/gorm
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)
|
stmt.SQL.Grow(stmt.ReflectValue.Len() * 15)
|
||||||
values.Values = make([][]interface{}, stmt.ReflectValue.Len())
|
values.Values = make([][]interface{}, stmt.ReflectValue.Len())
|
||||||
defaultValueFieldsHavingValue := map[*schema.Field][]interface{}{}
|
defaultValueFieldsHavingValue := map[*schema.Field][]interface{}{}
|
||||||
|
if stmt.ReflectValue.Len() == 0 {
|
||||||
|
stmt.AddError(gorm.ErrEmptySlice)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
for i := 0; i < stmt.ReflectValue.Len(); i++ {
|
for i := 0; i < stmt.ReflectValue.Len(); i++ {
|
||||||
rv := reflect.Indirect(stmt.ReflectValue.Index(i))
|
rv := reflect.Indirect(stmt.ReflectValue.Index(i))
|
||||||
values.Values[i] = make([]interface{}, len(values.Columns))
|
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)
|
selectColumns, restricted = stmt.SelectAndOmitColumns(true, false)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if len(mapValues) == 0 {
|
||||||
|
stmt.AddError(gorm.ErrEmptySlice)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
for idx, mapValue := range mapValues {
|
for idx, mapValue := range mapValues {
|
||||||
for k, v := range mapValue {
|
for k, v := range mapValue {
|
||||||
if stmt.Schema != nil {
|
if stmt.Schema != nil {
|
||||||
|
|
|
@ -27,4 +27,6 @@ var (
|
||||||
ErrRegistered = errors.New("registered")
|
ErrRegistered = errors.New("registered")
|
||||||
// ErrInvalidField invalid field
|
// ErrInvalidField invalid field
|
||||||
ErrInvalidField = errors.New("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) {
|
func TestCreateWithExistingTimestamp(t *testing.T) {
|
||||||
user := User{Name: "CreateUserExistingTimestamp"}
|
user := User{Name: "CreateUserExistingTimestamp"}
|
||||||
curTime := now.MustParse("2016-01-01")
|
curTime := now.MustParse("2016-01-01")
|
||||||
|
|
|
@ -16,3 +16,5 @@ require (
|
||||||
replace gorm.io/gorm => ../
|
replace gorm.io/gorm => ../
|
||||||
|
|
||||||
replace github.com/jackc/pgx/v4 => github.com/jinzhu/pgx/v4 v4.8.2
|
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