

求助一个问题,关于协程和gorm的
source link: https://studygolang.com/articles/36166?fr=sidebar
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

一切都源于我开发中发现的一个bug,简单描述就是 先insert 了一条数据,然后开启了一个协程,直接进行查询刚才插入的数据,竟然没查到。
func Test(t *testing.T) {
record := &model.ContentCoursePracticeRecord{
Cid: 0,
Uid: 1,
Complete: 1,
Type: 2,
Duration: 100,
Ctime: time.Now(),
Uptime: time.Now(),
}
err := dao.ContentCoursePracticeRecordDao.Create(record)
assert.Equal(t, err, nil)
go func() {
// 查询0元购活动
var (
duration int
)
db, err := utils.GetDB()
if err != nil {
assert.Equal(t, err, nil)
}
if ret := db.Model(&model.ContentCoursePracticeRecord{}).
Where("isdel = 0 and uid = ?", 1).
Where("ctime >= ?", time.Now().Format("2006-01-02")).
Select("sum(duration) as duration").
Find(&duration); ret.Error != nil {
// 此处报错 ,说明没有数据
//sql: Scan error on column index 0, name "duration": converting NULL to int is unsupported
assert.Equal(t, err, nil)
}
}()
}
他不是一个必现的问题,我也没有成功复现出来 后来把查询从写成挪出去就没有问题了,但是也没有想明白问题?望各位大佬指点!
Recommend
-
8
使用gorm不当出现too Many Connections的问题 2020-01-02 18:00:35 code 约 616 字 预计阅读 2 分钟...
-
12
...
-
3
公司的项目需要用到一个国外的开源 HTML5 项目,可以理解为是一套游戏的底层引擎库+几十个小游戏。使用的方式很简单,在项目里做一个小浏览器,直接在本地打开这些 HTML5 页面。 这个项目的具体情况如下: 第一个部分:已经编译好的独立 H...
-
8
V2EX › Python 关于安装 pydensecrf 的问题求助 tg11 · 11 小时 48 分钟前 · 128 次点击 ...
-
9
gorm 查询总是将表名转为复数的问题 2022年5月24日 这几天在用GoLang写个东西,期间用到了
-
8
求助,关于json反序列化问题 DG9Jww · 大约6小时之前 · 115 次点击 ·...
-
6
V2EX › Linux 关于 nginx 四层转发问题求助 cndns...
-
5
V2EX › 硬件 有个关于交换机风扇的问题想求助一下 v 友们
-
3
V2EX › macOS [真心求助贴] 关于 Mac m1 air 充电头的问题
-
10
V2EX › 宽带症候群 求助关于 ros v7 获取不了 ipv6 前缀的问题
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK