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

相关推荐
木棉知行者7 小时前
【第2篇】RuntimeError: nms_impl: implementation for device cuda:0 not found.
人工智能·深度学习·bug·mmdetection
DB!!!8 小时前
【bug】系统CA证书毁坏导致找不到有效的 OpenPGP 数据
bug
中冕—霍格沃兹软件开发测试21 小时前
测试用例库建设与管理方案
数据库·人工智能·科技·开源·测试用例·bug
lvchaoq1 天前
解决组件不能远程搜索的问题
前端·bug
Vanranrr1 天前
一个由非虚函数导致的隐藏Bug:窗口显示异常问题排查与解决
开发语言·bug
AllinGold1 天前
vscode等IDE使用ssh远程连接云服务器ECS,连接不上的通常问题
bug
测试人社区—84162 天前
Postman API测试指南
人工智能·git·测试工具·自动化·bug·postman
万粉变现经纪人2 天前
如何解决 pip install 网络报错 403 Forbidden(访问被阻止)问题
数据库·python·pycharm·beautifulsoup·bug·pandas·pip
测试人社区—84162 天前
Appium移动自动化测试:从入门到精通实战
人工智能·安全·appium·自动化·bug·postman
Wokoo72 天前
软件测试分类与BUG管理
功能测试·单元测试·bug·集成测试·压力测试·ab测试