侧边栏壁纸
博主头像
Tony's Blog博主等级

行动起来,coding

  • 累计撰写 83 篇文章
  • 累计创建 58 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录
go

gorm查询报:sql_driver_ couldn‘t convert “_x00“ into type bool;

Tony
2024-02-21 / 0 评论 / 0 点赞 / 16 阅读 / 5556 字

本文链接: https://blog.csdn.net/lishuangquan1987/article/details/123710644

版本:

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

1-zdgc.png
查询代码:

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)

报错如下:

2-pfyl.png

查询资料,都显示是 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"`
}

再进行查询就不会报错啦

0

评论区