【BUG】golang gorm导入数据库报错 “unexpected type clause.Expr“

帮同事排查一个gorm导入数据报错的问题

事发现场

ck sql

sql 复制代码
CREATE TABLE ods_api.t_sms_jg_msg_callback_dis
(
    `app_key` String DEFAULT '' COMMENT '应用标识',
    `callback_type` Int32 DEFAULT 0 COMMENT '0送达,1回执',
    `channel` Int32 DEFAULT 0 COMMENT 'uid下发的渠道',
    `model` String DEFAULT '' COMMENT '设备机型',
    `jgid` String DEFAULT '' COMMENT '极光返回的msgid',
    `notification_state` Int8 DEFAULT 0 COMMENT '接收通知时通知开关是否打开',
    `params` String DEFAULT '' COMMENT '用户在push API推送请求的时候在payload的callback里自行指定的参数',
    `platform` String DEFAULT '' COMMENT '推送平台',
    `registration_id` String DEFAULT '' COMMENT '设备唯一标识',
    `alias` String DEFAULT '' COMMENT '设备registration_id对应的别名',
    `send_time` String DEFAULT '' COMMENT '通知的送达时间/ 用户点击通知的时间',
    `created_at` DateTime DEFAULT now()
)
ENGINE = ReplacingMergeTree(created_at)
PARTITION BY toDate(created_at)
ORDER BY (callback_type, jgid, registration_id, send_time)
SETTINGS index_granularity = 8192
COMMENT '极光push点击/送达回执记录表,中台notify服务用'

go 数据表

go 复制代码
type TSmsJgMsgCallback struct {
	AppKey            string    `gorm:"column:app_key;default:;comment:'应用标识'" json:"appkey"`
	CallbackType      int32     `gorm:"column:callback_type;default:0;comment:'0送达,1回执'" json:"callback_type"`
	Channel           int32     `gorm:"column:channel;default:0;comment:'uid下发的渠道'" json:"channel"`
	Model             string    `gorm:"column:model;default:;comment:'设备机型'" json:"model"`
	Jgid              string    `gorm:"column:jgid;default:;comment:'极光返回的msgid'" json:"msgid"`
	NotificationState int8      `gorm:"column:notification_state;default:0;comment:'接收通知时通知开关是否打开'" json:"notification_state"`
	Params            string    `gorm:"column:params;default:;comment:'用户在push API推送请求的时候在payload的callback里自行指定的参数'" json:"params"`
	Platform          string    `gorm:"column:platform;default:;comment:'推送平台'" json:"platform"`
	RegistrationId    string    `gorm:"column:registration_id;default:;comment:'设备唯一标识'" json:"registration_id"`
	Alias             string    `gorm:"column:alias;default:;comment:'设备registration_id对应的别名'" json:"alias"`
	SendTime          string    `gorm:"column:send_time;default:;comment:'通知的送达时间/ 用户点击通知的时间'" json:"send_time"`
	CreatedAt         time.Time `gorm:"column:created_at"`
}

func (t *TSmsJgMsgCallback) TableName() string {
	return "t_sms_jg_msg_callback_dis"
}

导入数据报错代码

go 复制代码
func insertCk(callbackList []ck.TSmsJgMsgCallback) {
	if len(callbackList) == 0 {
		return
	}
	err := clickhouse.GetPushCk().Table((&ck.TSmsJgMsgCallback{}).TableName()).Create(&callbackList).Error
	if err != nil {
		logger.Error("callbackClick insertCk err", zap.Error(err), zap.Any("callbackList", callbackList))
		return
	}

日志打印:

复制代码
 INSERT INTO `t_sms_jg_msg_callback` (`callback_type`,`channel`,`notification_state`,`alias`,`send_time`,`app_key`,`model`,`jgid`,`params`,`platform`,`registration_id`) VALUES (1,1,1,'alias1','1510109259','b2d7e0fbaa7f4d7e9c2b3bc0','model1','123','{"key1":"value1","key2":"value2"}','android','1a0018970a4b4b4b4b4b4b4b4b4b4b4b')
 callbackClick ck insert err     {"error": "alias (String): unexpected type clause.Expr"

问题分析

从sql日志和手动执行,并无问题,只能找gorm clihouse源码debug分析为什么报类型不一致

可以看到,变量里jgid字段类型不为string,而是为DEFAULT?

变量类型为[]interface{} ?

再看gorm的定义

gorm语法写错了

总结

通过SQL自动生成自动代码model时,尽量用公司内部的组件或者可靠的三方工具,自己写非常容易出错

相关推荐
这个软件需要设计一下2 天前
ninedata安装磁盘不足问题解决
运维·bug
热爱生活的五柒2 天前
cc-switch安装方法、介绍及遇到的bug
bug·cc-switch
Greenland_122 天前
Android 混淆与混淆后bug日志问题定位
android·bug
应用市场2 天前
踩坑记录:有符号整数位运算的那些隐蔽Bug——符号扩展、算术右移与补码
java·开发语言·bug
一灰灰blog3 天前
Jar包会自己消失?Excel会“记忆“数据?我遇到了两个灵异bug
java·spring boot·bug·excel
王家视频教程图书馆4 天前
修复服务端500相应,修复客户端上传文件.tmp 服务端接受不到文件bug
bug
qq_401700414 天前
Qt开发过程中遇到哪些经典的bug
qt·bug
0白露6 天前
关闭搜狗输入法右下角广告,可以适用于大多数应用系统通知的广告
windows·bug
一只自律的鸡6 天前
【Linux驱动】bug处理 ens33找不到IP
linux·运维·bug
Lichenpar8 天前
Springboot采用FastJson2作为MessageConverter时,配置的全局日期类型序列化转换BUG
java·开发语言·bug