forked from mirror/gorm
Handle field set value error
This commit is contained in:
parent
a7b3b5956f
commit
f92e6747cb
|
@ -159,9 +159,9 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) {
|
||||||
for _, ref := range rel.References {
|
for _, ref := range rel.References {
|
||||||
if ref.OwnPrimaryKey {
|
if ref.OwnPrimaryKey {
|
||||||
fv, _ := ref.PrimaryKey.ValueOf(db.Statement.Context, db.Statement.ReflectValue)
|
fv, _ := ref.PrimaryKey.ValueOf(db.Statement.Context, db.Statement.ReflectValue)
|
||||||
ref.ForeignKey.Set(db.Statement.Context, f, fv)
|
db.AddError(ref.ForeignKey.Set(db.Statement.Context, f, fv))
|
||||||
} else if ref.PrimaryValue != "" {
|
} else if ref.PrimaryValue != "" {
|
||||||
ref.ForeignKey.Set(db.Statement.Context, f, ref.PrimaryValue)
|
db.AddError(ref.ForeignKey.Set(db.Statement.Context, f, ref.PrimaryValue))
|
||||||
}
|
}
|
||||||
assignmentColumns = append(assignmentColumns, ref.ForeignKey.DBName)
|
assignmentColumns = append(assignmentColumns, ref.ForeignKey.DBName)
|
||||||
}
|
}
|
||||||
|
@ -193,9 +193,9 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) {
|
||||||
for _, ref := range rel.References {
|
for _, ref := range rel.References {
|
||||||
if ref.OwnPrimaryKey {
|
if ref.OwnPrimaryKey {
|
||||||
pv, _ := ref.PrimaryKey.ValueOf(db.Statement.Context, v)
|
pv, _ := ref.PrimaryKey.ValueOf(db.Statement.Context, v)
|
||||||
ref.ForeignKey.Set(db.Statement.Context, elem, pv)
|
db.AddError(ref.ForeignKey.Set(db.Statement.Context, elem, pv))
|
||||||
} else if ref.PrimaryValue != "" {
|
} else if ref.PrimaryValue != "" {
|
||||||
ref.ForeignKey.Set(db.Statement.Context, elem, ref.PrimaryValue)
|
db.AddError(ref.ForeignKey.Set(db.Statement.Context, elem, ref.PrimaryValue))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -261,12 +261,12 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) {
|
||||||
for _, ref := range rel.References {
|
for _, ref := range rel.References {
|
||||||
if ref.OwnPrimaryKey {
|
if ref.OwnPrimaryKey {
|
||||||
fv, _ := ref.PrimaryKey.ValueOf(db.Statement.Context, obj)
|
fv, _ := ref.PrimaryKey.ValueOf(db.Statement.Context, obj)
|
||||||
ref.ForeignKey.Set(db.Statement.Context, joinValue, fv)
|
db.AddError(ref.ForeignKey.Set(db.Statement.Context, joinValue, fv))
|
||||||
} else if ref.PrimaryValue != "" {
|
} else if ref.PrimaryValue != "" {
|
||||||
ref.ForeignKey.Set(db.Statement.Context, joinValue, ref.PrimaryValue)
|
db.AddError(ref.ForeignKey.Set(db.Statement.Context, joinValue, ref.PrimaryValue))
|
||||||
} else {
|
} else {
|
||||||
fv, _ := ref.PrimaryKey.ValueOf(db.Statement.Context, elem)
|
fv, _ := ref.PrimaryKey.ValueOf(db.Statement.Context, elem)
|
||||||
ref.ForeignKey.Set(db.Statement.Context, joinValue, fv)
|
db.AddError(ref.ForeignKey.Set(db.Statement.Context, joinValue, fv))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
joins = reflect.Append(joins, joinValue)
|
joins = reflect.Append(joins, joinValue)
|
||||||
|
|
|
@ -121,7 +121,7 @@ func Create(config *Config) func(db *gorm.DB) {
|
||||||
|
|
||||||
_, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(db.Statement.Context, rv)
|
_, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(db.Statement.Context, rv)
|
||||||
if isZero {
|
if isZero {
|
||||||
db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.Context, rv, insertID)
|
db.AddError(db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.Context, rv, insertID))
|
||||||
insertID -= db.Statement.Schema.PrioritizedPrimaryField.AutoIncrementIncrement
|
insertID -= db.Statement.Schema.PrioritizedPrimaryField.AutoIncrementIncrement
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -133,7 +133,7 @@ func Create(config *Config) func(db *gorm.DB) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(db.Statement.Context, rv); isZero {
|
if _, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(db.Statement.Context, rv); isZero {
|
||||||
db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.Context, rv, insertID)
|
db.AddError(db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.Context, rv, insertID))
|
||||||
insertID += db.Statement.Schema.PrioritizedPrimaryField.AutoIncrementIncrement
|
insertID += db.Statement.Schema.PrioritizedPrimaryField.AutoIncrementIncrement
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -141,7 +141,7 @@ func Create(config *Config) func(db *gorm.DB) {
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
_, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(db.Statement.Context, db.Statement.ReflectValue)
|
_, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(db.Statement.Context, db.Statement.ReflectValue)
|
||||||
if isZero {
|
if isZero {
|
||||||
db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.Context, db.Statement.ReflectValue, insertID)
|
db.AddError(db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.Context, db.Statement.ReflectValue, insertID))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -227,13 +227,13 @@ func ConvertToCreateValues(stmt *gorm.Statement) (values clause.Values) {
|
||||||
if values.Values[i][idx], isZero = field.ValueOf(stmt.Context, rv); isZero {
|
if values.Values[i][idx], isZero = field.ValueOf(stmt.Context, rv); isZero {
|
||||||
if field.DefaultValueInterface != nil {
|
if field.DefaultValueInterface != nil {
|
||||||
values.Values[i][idx] = field.DefaultValueInterface
|
values.Values[i][idx] = field.DefaultValueInterface
|
||||||
field.Set(stmt.Context, rv, field.DefaultValueInterface)
|
stmt.AddError(field.Set(stmt.Context, rv, field.DefaultValueInterface))
|
||||||
} else if field.AutoCreateTime > 0 || field.AutoUpdateTime > 0 {
|
} else if field.AutoCreateTime > 0 || field.AutoUpdateTime > 0 {
|
||||||
field.Set(stmt.Context, rv, curTime)
|
stmt.AddError(field.Set(stmt.Context, rv, curTime))
|
||||||
values.Values[i][idx], _ = field.ValueOf(stmt.Context, rv)
|
values.Values[i][idx], _ = field.ValueOf(stmt.Context, rv)
|
||||||
}
|
}
|
||||||
} else if field.AutoUpdateTime > 0 && updateTrackTime {
|
} else if field.AutoUpdateTime > 0 && updateTrackTime {
|
||||||
field.Set(stmt.Context, rv, curTime)
|
stmt.AddError(field.Set(stmt.Context, rv, curTime))
|
||||||
values.Values[i][idx], _ = field.ValueOf(stmt.Context, rv)
|
values.Values[i][idx], _ = field.ValueOf(stmt.Context, rv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -267,13 +267,13 @@ func ConvertToCreateValues(stmt *gorm.Statement) (values clause.Values) {
|
||||||
if values.Values[0][idx], isZero = field.ValueOf(stmt.Context, stmt.ReflectValue); isZero {
|
if values.Values[0][idx], isZero = field.ValueOf(stmt.Context, stmt.ReflectValue); isZero {
|
||||||
if field.DefaultValueInterface != nil {
|
if field.DefaultValueInterface != nil {
|
||||||
values.Values[0][idx] = field.DefaultValueInterface
|
values.Values[0][idx] = field.DefaultValueInterface
|
||||||
field.Set(stmt.Context, stmt.ReflectValue, field.DefaultValueInterface)
|
stmt.AddError(field.Set(stmt.Context, stmt.ReflectValue, field.DefaultValueInterface))
|
||||||
} else if field.AutoCreateTime > 0 || field.AutoUpdateTime > 0 {
|
} else if field.AutoCreateTime > 0 || field.AutoUpdateTime > 0 {
|
||||||
field.Set(stmt.Context, stmt.ReflectValue, curTime)
|
stmt.AddError(field.Set(stmt.Context, stmt.ReflectValue, curTime))
|
||||||
values.Values[0][idx], _ = field.ValueOf(stmt.Context, stmt.ReflectValue)
|
values.Values[0][idx], _ = field.ValueOf(stmt.Context, stmt.ReflectValue)
|
||||||
}
|
}
|
||||||
} else if field.AutoUpdateTime > 0 && updateTrackTime {
|
} else if field.AutoUpdateTime > 0 && updateTrackTime {
|
||||||
field.Set(stmt.Context, stmt.ReflectValue, curTime)
|
stmt.AddError(field.Set(stmt.Context, stmt.ReflectValue, curTime))
|
||||||
values.Values[0][idx], _ = field.ValueOf(stmt.Context, stmt.ReflectValue)
|
values.Values[0][idx], _ = field.ValueOf(stmt.Context, stmt.ReflectValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -123,17 +123,17 @@ func preload(tx *gorm.DB, rel *schema.Relationship, conds []interface{}, preload
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
switch rel.Type {
|
switch rel.Type {
|
||||||
case schema.HasMany, schema.Many2Many:
|
case schema.HasMany, schema.Many2Many:
|
||||||
rel.Field.Set(tx.Statement.Context, reflectValue, reflect.MakeSlice(rel.Field.IndirectFieldType, 0, 10).Interface())
|
tx.AddError(rel.Field.Set(tx.Statement.Context, reflectValue, reflect.MakeSlice(rel.Field.IndirectFieldType, 0, 10).Interface()))
|
||||||
default:
|
default:
|
||||||
rel.Field.Set(tx.Statement.Context, reflectValue, reflect.New(rel.Field.FieldType).Interface())
|
tx.AddError(rel.Field.Set(tx.Statement.Context, reflectValue, reflect.New(rel.Field.FieldType).Interface()))
|
||||||
}
|
}
|
||||||
case reflect.Slice, reflect.Array:
|
case reflect.Slice, reflect.Array:
|
||||||
for i := 0; i < reflectValue.Len(); i++ {
|
for i := 0; i < reflectValue.Len(); i++ {
|
||||||
switch rel.Type {
|
switch rel.Type {
|
||||||
case schema.HasMany, schema.Many2Many:
|
case schema.HasMany, schema.Many2Many:
|
||||||
rel.Field.Set(tx.Statement.Context, reflectValue.Index(i), reflect.MakeSlice(rel.Field.IndirectFieldType, 0, 10).Interface())
|
tx.AddError(rel.Field.Set(tx.Statement.Context, reflectValue.Index(i), reflect.MakeSlice(rel.Field.IndirectFieldType, 0, 10).Interface()))
|
||||||
default:
|
default:
|
||||||
rel.Field.Set(tx.Statement.Context, reflectValue.Index(i), reflect.New(rel.Field.FieldType).Interface())
|
tx.AddError(rel.Field.Set(tx.Statement.Context, reflectValue.Index(i), reflect.New(rel.Field.FieldType).Interface()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -158,12 +158,12 @@ func preload(tx *gorm.DB, rel *schema.Relationship, conds []interface{}, preload
|
||||||
reflectFieldValue = reflect.Indirect(reflectFieldValue)
|
reflectFieldValue = reflect.Indirect(reflectFieldValue)
|
||||||
switch reflectFieldValue.Kind() {
|
switch reflectFieldValue.Kind() {
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
rel.Field.Set(tx.Statement.Context, data, elem.Interface())
|
tx.AddError(rel.Field.Set(tx.Statement.Context, data, elem.Interface()))
|
||||||
case reflect.Slice, reflect.Array:
|
case reflect.Slice, reflect.Array:
|
||||||
if reflectFieldValue.Type().Elem().Kind() == reflect.Ptr {
|
if reflectFieldValue.Type().Elem().Kind() == reflect.Ptr {
|
||||||
rel.Field.Set(tx.Statement.Context, data, reflect.Append(reflectFieldValue, elem).Interface())
|
tx.AddError(rel.Field.Set(tx.Statement.Context, data, reflect.Append(reflectFieldValue, elem).Interface()))
|
||||||
} else {
|
} else {
|
||||||
rel.Field.Set(tx.Statement.Context, data, reflect.Append(reflectFieldValue, elem.Elem()).Interface())
|
tx.AddError(rel.Field.Set(tx.Statement.Context, data, reflect.Append(reflectFieldValue, elem.Elem()).Interface()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ func SetupUpdateReflectValue(db *gorm.DB) {
|
||||||
if dest, ok := db.Statement.Dest.(map[string]interface{}); ok {
|
if dest, ok := db.Statement.Dest.(map[string]interface{}); ok {
|
||||||
for _, rel := range db.Statement.Schema.Relationships.BelongsTo {
|
for _, rel := range db.Statement.Schema.Relationships.BelongsTo {
|
||||||
if _, ok := dest[rel.Name]; ok {
|
if _, ok := dest[rel.Name]; ok {
|
||||||
rel.Field.Set(db.Statement.Context, db.Statement.ReflectValue, dest[rel.Name])
|
db.AddError(rel.Field.Set(db.Statement.Context, db.Statement.ReflectValue, dest[rel.Name]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
4
scan.go
4
scan.go
|
@ -69,7 +69,7 @@ func (db *DB) scanIntoStruct(rows *sql.Rows, reflectValue reflect.Value, values
|
||||||
for idx, field := range fields {
|
for idx, field := range fields {
|
||||||
if field != nil {
|
if field != nil {
|
||||||
if len(joinFields) == 0 || joinFields[idx][0] == nil {
|
if len(joinFields) == 0 || joinFields[idx][0] == nil {
|
||||||
field.Set(db.Statement.Context, reflectValue, values[idx])
|
db.AddError(field.Set(db.Statement.Context, reflectValue, values[idx]))
|
||||||
} else {
|
} else {
|
||||||
relValue := joinFields[idx][0].ReflectValueOf(db.Statement.Context, reflectValue)
|
relValue := joinFields[idx][0].ReflectValueOf(db.Statement.Context, reflectValue)
|
||||||
if relValue.Kind() == reflect.Ptr && relValue.IsNil() {
|
if relValue.Kind() == reflect.Ptr && relValue.IsNil() {
|
||||||
|
@ -79,7 +79,7 @@ func (db *DB) scanIntoStruct(rows *sql.Rows, reflectValue reflect.Value, values
|
||||||
|
|
||||||
relValue.Set(reflect.New(relValue.Type().Elem()))
|
relValue.Set(reflect.New(relValue.Type().Elem()))
|
||||||
}
|
}
|
||||||
joinFields[idx][1].Set(db.Statement.Context, relValue, values[idx])
|
db.AddError(joinFields[idx][1].Set(db.Statement.Context, relValue, values[idx]))
|
||||||
}
|
}
|
||||||
|
|
||||||
// release data to pool
|
// release data to pool
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jinzhu/now"
|
"github.com/jinzhu/now"
|
||||||
|
"gorm.io/gorm/clause"
|
||||||
"gorm.io/gorm/utils"
|
"gorm.io/gorm/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -567,8 +568,8 @@ func (field *Field) setupValuerAndSetter() {
|
||||||
if v, err = valuer.Value(); err == nil {
|
if v, err = valuer.Value(); err == nil {
|
||||||
err = setter(ctx, value, v)
|
err = setter(ctx, value, v)
|
||||||
}
|
}
|
||||||
} else {
|
} else if _, ok := v.(clause.Expr); !ok {
|
||||||
return fmt.Errorf("failed to set value %+v to field %s", v, field.Name)
|
return fmt.Errorf("failed to set value %#v to field %s", v, field.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -562,7 +562,7 @@ func (stmt *Statement) SetColumn(name string, value interface{}, fromCallbacks .
|
||||||
|
|
||||||
switch destValue.Kind() {
|
switch destValue.Kind() {
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
field.Set(stmt.Context, destValue, value)
|
stmt.AddError(field.Set(stmt.Context, destValue, value))
|
||||||
default:
|
default:
|
||||||
stmt.AddError(ErrInvalidData)
|
stmt.AddError(ErrInvalidData)
|
||||||
}
|
}
|
||||||
|
@ -572,10 +572,10 @@ func (stmt *Statement) SetColumn(name string, value interface{}, fromCallbacks .
|
||||||
case reflect.Slice, reflect.Array:
|
case reflect.Slice, reflect.Array:
|
||||||
if len(fromCallbacks) > 0 {
|
if len(fromCallbacks) > 0 {
|
||||||
for i := 0; i < stmt.ReflectValue.Len(); i++ {
|
for i := 0; i < stmt.ReflectValue.Len(); i++ {
|
||||||
field.Set(stmt.Context, stmt.ReflectValue.Index(i), value)
|
stmt.AddError(field.Set(stmt.Context, stmt.ReflectValue.Index(i), value))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
field.Set(stmt.Context, stmt.ReflectValue.Index(stmt.CurDestIndex), value)
|
stmt.AddError(field.Set(stmt.Context, stmt.ReflectValue.Index(stmt.CurDestIndex), value))
|
||||||
}
|
}
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
if !stmt.ReflectValue.CanAddr() {
|
if !stmt.ReflectValue.CanAddr() {
|
||||||
|
@ -583,7 +583,7 @@ func (stmt *Statement) SetColumn(name string, value interface{}, fromCallbacks .
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
field.Set(stmt.Context, stmt.ReflectValue, value)
|
stmt.AddError(field.Set(stmt.Context, stmt.ReflectValue, value))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
stmt.AddError(ErrInvalidField)
|
stmt.AddError(ErrInvalidField)
|
||||||
|
|
|
@ -9,7 +9,7 @@ require (
|
||||||
github.com/jinzhu/now v1.1.5
|
github.com/jinzhu/now v1.1.5
|
||||||
github.com/lib/pq v1.10.4
|
github.com/lib/pq v1.10.4
|
||||||
github.com/mattn/go-sqlite3 v1.14.12 // indirect
|
github.com/mattn/go-sqlite3 v1.14.12 // indirect
|
||||||
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
|
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 // indirect
|
||||||
gorm.io/driver/mysql v1.3.2
|
gorm.io/driver/mysql v1.3.2
|
||||||
gorm.io/driver/postgres v1.3.1
|
gorm.io/driver/postgres v1.3.1
|
||||||
gorm.io/driver/sqlite v1.3.1
|
gorm.io/driver/sqlite v1.3.1
|
||||||
|
|
Loading…
Reference in New Issue