Gorm FirstOrCreate理解

时间:2014-07-10 12:33:56

标签: mysql go go-gorm revel

我是golanggorm中的新人,我试图了解FirstOrCreate的工作原理。我有模特:

type Setting struct {
    Id        int64
    Email     string
    Phone     string
    Skype     string
    Country   string
    City      string
    Address   string
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt time.Time
}

和下一个片段:

func (c Admins) ShowSettings() revel.Result {    
    var setting models.Setting
    DB.FirstOrCreate(&setting, models.Setting{})

    return c.Render(setting)
}

我第一次运行此功能时,一切正常,它必须在数据库中创建新实体,但是当我下次调用此函数来显示此setting时,我有错误:< / p>

runtime error: invalid memory address or nil pointer dereference

哪里出错?

编辑: 完整错误:

ERROR 2014/07/11 08:16:34 panic.go:29: runtime error: invalid memory address or nil pointer dereference
/home/cnaize/Dropbox/develop/gocode/src/advorts/app/controllers/admins.go:99 (0x472bdf)
    Admins.ShowSettings: DB.FirstOrCreate(&setting, models.Setting{})
/home/cnaize/Dropbox/develop/gocode/src/advorts/app/controllers/admins.go:1 (0x4765ce)
    (*Admins).ShowSettings: package controllers
/usr/lib/go/src/pkg/runtime/asm_amd64.s:339 (0x426e62)
    call32: CALLFN(call32, 32)
/usr/lib/go/src/pkg/reflect/value.go:474 (0x43488b)
    Value.call: call(fn, ptr, uint32(size))
/usr/lib/go/src/pkg/reflect/value.go:345 (0x43397d)
    Value.Call: return v.call("Call", in)
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/invoker.go:35 (0x451580)
    com/revel/revel.ActionInvoker: resultValue = methodValue.Call(methodArgs)[0]
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/compress.go:45 (0x44699c)
    com/revel/revel.CompressFilter: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/intercept.go:103 (0x4500b0)
    com/revel/revel.InterceptorFilter: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/advorts/app/init.go:37 (0x471842)
    func.001: fc[0](c, fc[1:]) // Execute the next filter stage.
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/i18n.go:155 (0x44f70b)
    com/revel/revel.I18nFilter: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/validation.go:191 (0x4637d6)
    com/revel/revel.ValidationFilter: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/flash.go:45 (0x44cbbd)
    com/revel/revel.FlashFilter: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/session.go:144 (0x45e3d3)
    com/revel/revel.SessionFilter: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/params.go:133 (0x452e10)
    com/revel/revel.ParamsFilter: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/filterconfig.go:208 (0x44c87e)
    com/revel/revel.FilterConfiguringFilter: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/router.go:465 (0x45c456)
    com/revel/revel.RouterFilter: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/panic.go:15 (0x45190d)
    com/revel/revel.PanicFilter: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/watcher.go:160 (0x468201)
    com/revel/revel.func.029: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/server.go:42 (0x45ca6e)
    com/revel/revel.handleInternal: Filters[0](c, Filters[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/server.go:30 (0x45c7b4)
    com/revel/revel.handle: handleInternal(w, r, nil)
/usr/lib/go/src/pkg/net/http/server.go:1220 (0x4f0920)
    HandlerFunc.ServeHTTP: f(w, r)
/usr/lib/go/src/pkg/net/http/server.go:1597 (0x4f256e)
    serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/lib/go/src/pkg/net/http/server.go:1167 (0x4f0577)
    (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/lib/go/src/pkg/runtime/proc.c:1394 (0x41a650)
    goexit: runtime·goexit(void)

1 个答案:

答案 0 :(得分:0)

完全错误很明显:

ERROR 2014/07/11 08:16:34 panic.go:29: runtime error: invalid memory address or nil pointer dereference
/home/cnaize/Dropbox/develop/gocode/src/advorts/app/controllers/admins.go:99 (0x472bdf)
    Admins.ShowSettings: DB.FirstOrCreate(&setting, models.Setting{})

您的“ DB”为零 在尝试FirstOrCreate之前先启动数据库连接

db, err := gorm.Open("sqlite3", "test.db")
if err != nil {
    panic("failed to connect database")
}
defer db.Close()

// then you code
db.FirstOrCreate(&setting, models.Setting{})
...

或者也许您的“ DB”是您在应用启动时忘记了初始化的单例