diff --git a/contrib/drivers/pgsql/pgsql_convert.go b/contrib/drivers/pgsql/pgsql_convert.go index dc71a8cc40a..e040ebe10dc 100644 --- a/contrib/drivers/pgsql/pgsql_convert.go +++ b/contrib/drivers/pgsql/pgsql_convert.go @@ -24,12 +24,15 @@ func (d *Driver) ConvertValueForField(ctx context.Context, fieldType string, fie ) if fieldValueKind == reflect.Slice { - fieldValue = gstr.ReplaceByMap(gconv.String(fieldValue), - map[string]string{ - "[": "{", - "]": "}", - }, - ) + // For pgsql, json or jsonb require '[]' + if !gstr.Contains(fieldType, "json") { + fieldValue = gstr.ReplaceByMap(gconv.String(fieldValue), + map[string]string{ + "[": "{", + "]": "}", + }, + ) + } } return d.Core.ConvertValueForField(ctx, fieldType, fieldValue) } diff --git a/contrib/drivers/pgsql/pgsql_z_unit_issue_test.go b/contrib/drivers/pgsql/pgsql_z_unit_issue_test.go index 492826c210c..795035b369c 100644 --- a/contrib/drivers/pgsql/pgsql_z_unit_issue_test.go +++ b/contrib/drivers/pgsql/pgsql_z_unit_issue_test.go @@ -103,3 +103,42 @@ func Test_Issue3632(t *testing.T) { t.AssertNil(err) }) } + +// https://github.com/gogf/gf/issues/3671 +func Test_Issue3671(t *testing.T) { + type SubMember struct { + Seven string + Eight int64 + } + type Member struct { + One []int64 `json:"one" orm:"one"` + Two [][]string `json:"two" orm:"two"` + Three []string `json:"three" orm:"three"` + Four []int64 `json:"four" orm:"four"` + Five []SubMember `json:"five" orm:"five"` + } + var ( + sqlText = gtest.DataContent("issues", "issue3671.sql") + table = fmt.Sprintf(`%s_%d`, TablePrefix+"issue3632", 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) + member = Member{ + One: []int64{1, 2, 3}, + Two: [][]string{{"a", "b"}, {"c", "d"}}, + Three: []string{"x", "y", "z"}, + Four: []int64{1, 2, 3}, + Five: []SubMember{{Seven: "1", Eight: 2}, {Seven: "3", Eight: 4}}, + } + ) + + _, err := dao.Ctx(ctx).Data(&member).Insert() + t.AssertNil(err) + }) +} diff --git a/contrib/drivers/pgsql/testdata/issues/issue3671.sql b/contrib/drivers/pgsql/testdata/issues/issue3671.sql new file mode 100644 index 00000000000..57d78d70c2b --- /dev/null +++ b/contrib/drivers/pgsql/testdata/issues/issue3671.sql @@ -0,0 +1,8 @@ +CREATE TABLE "public"."%s" +( + "one" int8[] NOT NULL, + "two" text[][] NOT NULL, + "three" jsonb, + "four" json, + "five" jsonb +);