5

求助一个问题,关于协程和gorm的

 1 year ago
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.
neoserver,ios ssh client

一切都源于我开发中发现的一个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)
        }
    }()
}

他不是一个必现的问题,我也没有成功复现出来 后来把查询从写成挪出去就没有问题了,但是也没有想明白问题?望各位大佬指点!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK