mirror of
https://github.com/gogf/gf.git
synced 2025-04-05 11:18:50 +08:00
fix(contrib/drivers/pgsql): fix insert error when data struct field has nil in PgSQL (#3679)
This commit is contained in:
parent
448df14860
commit
803cb5a0bc
@ -12,6 +12,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
@ -19,9 +20,11 @@ import (
|
||||
|
||||
// ConvertValueForField converts value to database acceptable value.
|
||||
func (d *Driver) ConvertValueForField(ctx context.Context, fieldType string, fieldValue interface{}) (interface{}, error) {
|
||||
var (
|
||||
fieldValueKind = reflect.TypeOf(fieldValue).Kind()
|
||||
)
|
||||
if g.IsNil(fieldValue) {
|
||||
return d.Core.ConvertValueForField(ctx, fieldType, fieldValue)
|
||||
}
|
||||
|
||||
var fieldValueKind = reflect.TypeOf(fieldValue).Kind()
|
||||
|
||||
if fieldValueKind == reflect.Slice {
|
||||
// For pgsql, json or jsonb require '[]'
|
||||
|
@ -142,3 +142,33 @@ func Test_Issue3671(t *testing.T) {
|
||||
t.AssertNil(err)
|
||||
})
|
||||
}
|
||||
|
||||
// https://github.com/gogf/gf/issues/3668
|
||||
func Test_Issue3668(t *testing.T) {
|
||||
type Issue3668 struct {
|
||||
Text interface{}
|
||||
Number interface{}
|
||||
}
|
||||
var (
|
||||
sqlText = gtest.DataContent("issues", "issue3668.sql")
|
||||
table = fmt.Sprintf(`%s_%d`, TablePrefix+"issue3668", gtime.TimestampNano())
|
||||
)
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(sqlText, table)); err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
dao = db.Model(table)
|
||||
data = Issue3668{
|
||||
Text: "我们都是自然的婴儿,卧在宇宙的摇篮里",
|
||||
Number: nil,
|
||||
}
|
||||
)
|
||||
_, err := dao.Ctx(ctx).
|
||||
Data(data).
|
||||
Insert()
|
||||
t.AssertNil(err)
|
||||
})
|
||||
}
|
||||
|
4
contrib/drivers/pgsql/testdata/issues/issue3668.sql
vendored
Normal file
4
contrib/drivers/pgsql/testdata/issues/issue3668.sql
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
CREATE TABLE "public"."%s" (
|
||||
"text" varchar(255) COLLATE "pg_catalog"."default",
|
||||
"number" int4
|
||||
);
|
Loading…
x
Reference in New Issue
Block a user