go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.92k stars 3.93k forks source link

mysql biginto to golang int64 or []uint8 #7201

Open kkk-zxx opened 1 month ago

kkk-zxx commented 1 month ago

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.

github-actions[bot] commented 1 month ago

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