Gorm FirstOr建立理解

I'm a new one in golang and gorm and I'm trying to understand how FirstOrCreate works. I have model:

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
}

and next snippet:

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

    return c.Render(setting)
}

First time when I've run this function, everything was ok, it have to created new entity in database, but when I call this function next times to show this setting I have the error:

runtime error: invalid memory address or nil pointer dereference

Where is mistake?

EDITED: Full error:

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)
duanhu7390
duanhu7390 我不会碰我的数据库,它是示例中的全局gorm变量。我无法将代码粘贴到游乐场,因为我正在使用Revel,而游乐场使用了我理解的纯go代码
6 年多之前 回复
dongyan2267
dongyan2267 您设法将数据库设为零?在操场上运行的例子会有所帮助...
6 年多之前 回复
dpx49470
dpx49470 在行DB.FirstOrCreate(&setting,models.Setting{})
6 年多之前 回复
dpi74187
dpi74187 缺少线路号:哪些线路故障?
6 年多之前 回复
duanla4959
duanla4959 我发布了完整的错误。您还需要什么代码段?
6 年多之前 回复
du512926
du512926 显示完整的代码和完整的错误。
6 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问