gorm查询报:sqldriver: couldn‘t convert “x00“ into type bool;

gorm查询报:sqldriver: couldn‘t convert “x00“ into type bool;,第1张

版本:

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类型,实现ValueScan方法,解决了:https://blog.csdn.net/qq_41359051/article/details/104352602

解决办法 自定义bool类型MyBool
type MyBool bool
MyBool实现 ValueScan方法
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"`
}

再进行查询就不会报错啦

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/990685.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存