gorm.io/driver/mysql v1.2.2
gorm.io/gorm v1.22.
type BatInfo struct {
BatId int `gorm:"primarykey;autoincrement;column:batid"`
LoginTime *time.Time `gorm:"column:logintime;index:index_logintime;index:index_logintime_traycode_batcode"`
TrayCode string `gorm:"column:traycode;index:index_traycode;index:index_logintime_traycode_batcode"`
BatCode string `gorm:"column:batcode;notnull;index:index_batcode;index:index_logintime_traycode_batcode"`
BatPos int `gorm:"column:batpos"`
ProjectId sql.NullInt32 `gorm:"column:projectid;index:index_projectid"`
FlowId sql.NullInt32 `gorm:"column:flowid"`
IsFlowFinish bool `gorm:"column:isflowfinish"`
IsUnbind bool `gorm:"column:isunbind"`
LotNo string `gorm:"column:logno;index:index_logno"`
}
这个表是早就建好了,不能改字段类型,用C#的ORM映射时,IsFlowFinish 定义为bool就没问题
提示的报错字段是IsFlowFinish
IsFlowFinish
对应的数据库字段类型是bit
,所以这里映射为bool
IsUnbind
对应的数据库字段类型是bit
,也映射为bool
查询代码:
Db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic(err)
}
sqlDB, err := Db.DB()
if err != nil {
panic(err)
}
sqlDB.SetMaxIdleConns(1000)
sqlDB.SetMaxOpenConns(100000)
sqlDB.SetConnMaxLifetime(-1)
models := make([]tablemodel.BatInfo, 0)
DB.Where("trayCode=?","123").Find(&models)
fmt.Println(models)
报错如下:
查询资料,都显示是database/sql
这个包的问题。
参考资料:https://github.com/go-gorm/gorm/issues/1432
https://github.com/go-sql-driver/mysql/issues/440
后来看到这篇文章后,自定义一个bool
类型,实现Value
和Scan
方法,解决了:https://blog.csdn.net/qq_41359051/article/details/104352602
MyBool
type MyBool bool
MyBool实现 Value
和Scan
方法
func (b MyBool) Value() (driver.Value, error) {
result := make([]byte, 1)
if b {
result[0] = byte(1)
} else {
result[0] = 0
}
return result, nil
}
func (b MyBool) Scan(v interface{}) error {
bytes := v.([]byte)
if bytes[0] == 0 {
b = false
} else {
b = true
}
return nil
}
将上述与数据库映射的类中的bool
类型改为MyBool
type BatInfo struct {
BatId int `gorm:"primarykey;autoincrement;column:batid"`
LoginTime *time.Time `gorm:"column:logintime;index:index_logintime;index:index_logintime_traycode_batcode"`
TrayCode string `gorm:"column:traycode;index:index_traycode;index:index_logintime_traycode_batcode"`
BatCode string `gorm:"column:batcode;notnull;index:index_batcode;index:index_logintime_traycode_batcode"`
BatPos int `gorm:"column:batpos"`
ProjectId sql.NullInt32 `gorm:"column:projectid;index:index_projectid"`
FlowId sql.NullInt32 `gorm:"column:flowid"`
IsFlowFinish MyBool `gorm:"column:isflowfinish"`
IsUnbind MyBool `gorm:"column:isunbind"`
LotNo string `gorm:"column:logno;index:index_logno"`
}
再进行查询就不会报错啦
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)