栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

gorm查询报:sql/driver: couldn‘t convert “x00“ into type bool;

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

gorm查询报:sql/driver: couldn‘t convert “x00“ into type bool;

版本:

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

解决办法 自定义bool类型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"`
}

再进行查询就不会报错啦

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/903884.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号