解决Go gorm踩过的坑
作者:持之以恒2016 时间:2024-04-25 15:30:06
使用gorm.Model后无法查询数据
Scan error on column index 1, name “created_at”
提示:
Scan error on column index 1, name “created_at”: unsupported Scan, storing driver.Value type []uint8
解决办法:
打开数据库的时候加上parseTime=true
root:123456@tcp(127.0.0.1:3306)/mapdb?charset=utf8&parseTime=true
补充:golang Gorm 的使用总结
建立结构体时可以通过 TableName来指定要查找的表名
func (CoinLog) TableName() string {
return "coin_log"
}
通过gorm的映射指定对应表的列
ID int64 `gorm:"column:id" json:"id"`
通过预加载可以实现各个模型之间的一对多关系,例如下面的代码,其中device结构体对应多个DeviceModular,DeviceModular又有多个CommWeimaqi
通过下面的查询语句可以查询出对应的相关联数据
db.SqlDB.Preload("DeviceModular", "modular_type=1").Preload("DeviceModular.CommWeimaqi").Find(&device)
gorm暂时不支持批量插入
可以通过下面的方式完成批量插入的功能
tx := db.SqlDB.Begin()
sqlStr := "INSERT INTO report_form (id,create_time,choose_count, device_fall_count,game_order_count,coin_count,member_count," +
"day_member_count,visit_count,lgz_coin_count,weimaqi_coin_count,store_id,real_coin_count,m_coin_count,coin_spec) VALUES "
vals := []interface{}{}
const rowSQL = "(?,?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?,?,?)"
var inserts []string
for _, elem := range reportForms {
inserts = append(inserts, rowSQL)
vals = append(vals, elem.ID, elem.CreateTime, elem.ChooseCount, elem.DeviceFallCount, elem.GameOrderCount, elem.CoinCount, elem.MemberCount, elem.DayMemberCount, elem.VisitCount, elem.LgzCoinCount, elem.WeimaqiCoinCount, elem.StoreId, elem.RealCoinCount, elem.MCoinCount, elem.CoinSpec)
}
sqlStr = sqlStr + strings.Join(inserts, ",")
err := tx.Exec(sqlStr, vals...).Error
if err != nil {
tx.Rollback()
fmt.Print(err)
}else {
tx.Commit()
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/wei242425445/article/details/99202062
标签:Go,gorm,踩坑
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python中DJANGO简单测试实例
2022-09-09 16:36:53
Python学习之字典的创建和使用
2023-12-14 17:43:30
![](https://img.aspxhome.com/file/2023/3/123643_0s.png)
Django前后端分离csrf token获取方式
2021-03-15 04:23:43
![](https://img.aspxhome.com/file/2023/3/99143_0s.jpg)
Ubuntu 18.04配置mysql以及配置远程连接的步骤
2024-01-14 14:54:39
![](https://img.aspxhome.com/file/2023/6/118476_0s.png)
Web表单设计:表单结构
2011-04-22 12:32:00
深入理解ES6中let和闭包
2024-05-28 15:41:25
Vue动态组件component标签的用法大全
2024-05-29 22:28:33
![](https://img.aspxhome.com/file/2023/0/123100_0s.png)
asp如何做一个随机密码产生器?
2010-07-12 18:57:00
布局篇(1)—If you love css …
2008-04-16 14:14:00
![](https://img.aspxhome.com/file/UploadPic/20084/16/2008416141658686s.jpg)
Jmeter并发执行Python 脚本的完整流程
2021-05-12 02:52:49
python求斐波那契数列示例分享
2022-07-20 19:32:10
asp如何在第10000名来访者访问时显示中奖页面?
2010-06-18 19:45:00
如何在 Matplotlib 中更改绘图背景的实现
2022-08-28 03:13:36
![](https://img.aspxhome.com/file/2023/8/67338_0s.png)
ASP中Cookies集合使用方法详解
2007-09-14 10:16:00
Python中jieba库的介绍与使用
2021-05-09 11:45:55
![](https://img.aspxhome.com/file/2023/6/99506_0s.png)
python循环控制之break和continue流程控制语句
2021-02-05 15:22:29
MySQL表设计优化与索引 (七)
2010-10-25 20:06:00
Python通过正则库爬取淘宝商品信息代码实例
2021-12-09 18:49:55
![](https://img.aspxhome.com/file/2023/5/75235_0s.jpg)
python 字符串的驻留机制及优缺点
2022-11-16 10:07:37
JavaScript数学对象Math操作数字的方法
2024-04-10 10:54:34