Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database basic type and basic type slice deserialization result error #4086

Open
cyjaysong opened this issue Dec 30, 2024 · 2 comments
Open
Assignees
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.

Comments

@cyjaysong
Copy link
Contributor

Go version

go version go1.23.4 darwin/arm64

GoFrame version

2.9.0

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

DROP TABLE IF EXISTS `proxy_param`;
CREATE TABLE `proxy_param` (
  `proxy_id` bigint NOT NULL,
  `recommend_ids` json DEFAULT NULL,
  `photos` json DEFAULT NULL,
  PRIMARY KEY (`proxy_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

INSERT INTO `proxy_param` (`proxy_id`, `recommend_ids`, `photos`) VALUES (1, '[584, 585]', 'null');
INSERT INTO `proxy_param` (`proxy_id`, `recommend_ids`, `photos`) VALUES (2, '[]', NULL);

模型映射为: 
type ProxyParam struct {
	ProxyId               int64    `json:"proxyId"              orm:"proxy_id"              description:""`
	RecommendIds []int64  `json:"recommendIds" orm:"recommend_ids" description:""`
	Photos                []string `json:"photos"              orm:"photos"                 description:""`
}
var proxyParamList []*entity.ProxyParam
err := dao.ProxyParam.Ctx(context.Background()).Scan(&proxyParamList)
fmt.Println(err) // nil
for _, param := range proxyParamList {
fmt.Printf("%+v\n", param)
}
//遍历打印机结果1: &{ProxyId:1 RecommendIds:[584 585] Photos:[null]}, 这里反序列化结果明显错误了,null为有效的json内容,虽然和数据库的NULL有所不同,但是 json的null 转为[]string 结果应该是 []
//遍历打印机结果2: &{ProxyId:2 RecommendIds:[] Photos:[]}
var proxyId int64
err := dao.ProxyParam.Ctx(context.Background()).Where("proxy_id", 1).
Fields("proxy_id").Scan(&proxyId)
fmt.Println(err) // element of parameter "pointer" for function Scan should type of struct/*struct/[]struct/[]*struc
fmt.Println(proxyId)

var proxyIds []int64
err := dao.ProxyParam.Ctx(context.Background()).Fields("proxy_id").Scan(&proxyIds)
fmt.Println(err) // nil 
fmt.Println(proxyIds) // [0 0]  内容错误,正确值是[1 2]

var recommendIds []int64
err := dao.ProxyParam.Ctx(context.Background()).Where("proxy_id", 1).
Fields("recommend_ids").Scan(&recommendIds)
fmt.Println(err) //nil,  没有错误
fmt.Println(recommendIds) //[0]    内容错误,正确值是[584 585]

What did you see happen?

得到的查询结果是错误

What did you expect to see?

正确的查询结果

@cyjaysong cyjaysong added the bug It is confirmed a bug, but don't worry, we'll handle it. label Dec 30, 2024
@Issues-translate-bot Issues-translate-bot changed the title 数据库基本类型和基本类型切片反序列化 结果错误 Database basic type and basic type slice deserialization result error Dec 30, 2024
@cyjaysong
Copy link
Contributor Author

cyjaysong commented Dec 30, 2024

package main

import (
	"context"
	"fmt"
	_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
	_ "github.com/gogf/gf/contrib/nosql/redis/v2"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	ctx := context.Background()
	_, _ = g.DB().Exec(ctx, "DROP TABLE IF EXISTS proxy_param")
	_, _ = g.DB().Exec(ctx, `CREATE TABLE proxy_param (
  proxy_id bigint NOT NULL,
  recommend_ids json DEFAULT NULL,
  photos json DEFAULT NULL,
  PRIMARY KEY (proxy_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci`)

	_, _ = g.DB().Exec(ctx, `INSERT INTO proxy_param (proxy_id, recommend_ids, photos) VALUES (1, '[584, 585]', 'null')`)
	_, _ = g.DB().Exec(ctx, `INSERT INTO proxy_param (proxy_id, recommend_ids, photos) VALUES (2, '[]', NULL)`)

	type ProxyParam struct {
		ProxyId      int64    `json:"proxyId"              orm:"proxy_id"              description:""`
		RecommendIds []int64  `json:"recommendIds" orm:"recommend_ids" description:""`
		Photos       []string `json:"photos"              orm:"photos"                 description:""`
	}

	var proxyParamList []*ProxyParam
	err := g.DB().Model("proxy_param").Ctx(ctx).Scan(&proxyParamList)
	fmt.Println(err) // nil
	for _, param := range proxyParamList {
		fmt.Printf("%+v\n", param)
	}
	//遍历打印机结果1: &{ProxyId:1 RecommendIds:[584 585] Photos:[null]}, 这里反序列化结果明显错误了,null为有效的json内容,虽然和数据库的NULL有所不同,但是 json的null 转为[]string 结果应该是 []
	//遍历打印机结果2: &{ProxyId:2 RecommendIds:[] Photos:[]}

	var proxyId int64
	err = g.DB().Model("proxy_param").Ctx(ctx).Where("proxy_id", 1).
		Fields("proxy_id").Scan(&proxyId)
	fmt.Println(err) // element of parameter "pointer" for function Scan should type of struct/*struct/[]struct/[]*struc
	fmt.Println(proxyId) // 0

	var proxyIds []int64
	err = g.DB().Model("proxy_param").Ctx(ctx).Fields("proxy_id").Scan(&proxyIds)
	fmt.Println(err)      // nil
	fmt.Println(proxyIds) // [0 0]  内容错误,正确值是[1 2]

	var recommendIds []int64
	err = g.DB().Model("proxy_param").Ctx(ctx).Where("proxy_id", 1).
		Fields("recommend_ids").Scan(&recommendIds)
	fmt.Println(err)          //nil,  没有错误
	fmt.Println(recommendIds) //[0]    内容错误,正确值是[584 585]
}

``

@gqcn
Copy link
Member

gqcn commented Jan 7, 2025

Let me see.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.
Projects
None yet
Development

No branches or pull requests

2 participants