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

mysql biginto to golang int64 or []uint8 #7201

Open
kkk-zxx opened this issue Sep 21, 2024 · 1 comment
Open

mysql biginto to golang int64 or []uint8 #7201

kkk-zxx opened this issue Sep 21, 2024 · 1 comment
Assignees
Labels

Comments

@kkk-zxx
Copy link

kkk-zxx commented Sep 21, 2024

Your Question

`type Status int

// Scan 实现 Scanner 接口,用于将数据库的值映射到枚举类型
func (s *Status) Scan(value any) error {
switch v := value.(type) {
case int64:
*s = Status(value.(int64))
case []uint8:
data := string(v)
parseInt, _ := strconv.ParseInt(data, 10, 64)
*s = Status(parseInt)
default:
return fmt.Errorf("unsupported scan type %T", value)
}
return nil
}

// Value 实现 Valuer 接口,用于将枚举类型映射为数据库的值
func (s Status) Value() (driver.Value, error) {
return s, nil
}

type Users struct {
Name string
Type Status
}

func main() {
dsn := "root:123456@tcp(127.0.0.1:3306)/go_test?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.New(mysql.Config{
DSN: dsn}), &gorm.Config{})
if err != nil {
panic(err)
}
err = db.AutoMigrate(&Users{})
if err != nil {
return
}
var use []Users
var uses []Users
db.Table("users").Where("id = ?", 1).Find(&use)
db.Find(&uses)
`

db.Table("users").Where("id = ?", 1).Find(&use) ------- Scan value.(type) == int64
db.Find(&uses) ------ Scan value.(type) == []uint8

The document you expected this should be explained

Expected answer

I want to know why the input parameters of the scan method are different after executing these two lines of code.

@kkk-zxx kkk-zxx added the type:question general questions label Sep 21, 2024
@github-actions github-actions bot added type:missing reproduction steps missing reproduction steps and removed type:question general questions labels Sep 21, 2024
Copy link

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

@kkk-zxx kkk-zxx closed this as completed Sep 21, 2024
@github-actions github-actions bot added type:question general questions and removed type:missing reproduction steps missing reproduction steps labels Sep 21, 2024
@kkk-zxx kkk-zxx reopened this Sep 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants