【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时,尽量用公司内部的组件或者可靠的三方工具,自己写非常容易出错

相关推荐
QuestLab14 小时前
维护 Hermes Agent CN 过程中的碎碎念,以及从bug上得到的一点点启发
bug
java修仙传17 小时前
Java 实习日记:一次 Excel 导入校验 Bug 的定位与数据更新逻辑优化
java·数据库·bug·excel·后端开发
当战神遇到编程17 小时前
软件测试基础入门:从 BUG 到测试用例设计完整指南
测试用例·bug
Bear on Toilet3 天前
3. BUG篇
bug
编程探索者小陈4 天前
【测试】之BUG篇
bug
棋宣4 天前
uni-app编译到微信小程序中,父传子props首次传递数据不接收的bug
微信小程序·uni-app·bug
wqdian_com4 天前
华为手机浏览器的一个bug
服务器·华为·bug
清水白石0085 天前
把事故变成护城河:如何设计回归测试,防止“订单重复创建”这类历史 Bug 卷土重来?
python·bug
njsgcs5 天前
c# solidworks createline 拉伸发现有微小两点间隙 导致拉伸变成薄壁特征 改bug画了6个小时 解决结果
c#·bug·solidworks
前端达人5 天前
第18课:实战案例二,线上紧急 Bug 修复全过程
bug