go-gorm / playground

GORM Playground (Please Create PR for this project to report issues)
MIT License
88 stars 666 forks source link

Multiple Records Query:index out of range [0] with length 0 #719

Open DunZane opened 3 months ago

DunZane commented 3 months ago

Explain your user case and expected results

我有一个Model为Conversation。我首先添加了两条记录,成功入库,但是我在使用下面的代码时进行多条记录查询时(user-1下有两条记录):

    var cs []*Conversation
    err := DB.Where("created_by = ?", "user-1").Find(&cs).Error
    if err != nil {
        t.Errorf("Failed, got error: %v", err)
    }
    t.Log(cs[0].ConversationId)

出错。报错信息为:

[0.036ms] [rows:0] SELECT * FROM `conversations` WHERE created_by = "user-1"
--- FAIL: TestGORM (0.00s)
panic: runtime error: index out of range [0] with length 0 [recovered]
    panic: runtime error: index out of range [0] with length 0

我尝试直接运行对应的sql,可以查询出记录。如何解决呢?