与beego的模型错误

时间:2016-08-05 16:06:50

标签: go orm model beego

我对beego很新。我开始一个小项目来发现并开始学习 框架。我使用bee命令创建项目:

bee   :1.4.1
beego :1.6.1
Go    :go version go1.6.3 linux/amd64

beego的版本是:

package models

import "github.com/astaxie/beego/orm"

type ShortUrl struct {
    Id     int `orm:"auto"`
    Domain string
    Short  string
}

func init() {
    orm.RegisterModel(new(ShortUrl))
}

我有一个小模型,我的模型文件的内容是:

package main

import (
    "github.com/astaxie/beego"
    "github.com/astaxie/beego/orm"
    _ "github.com/hobbestigrou/mahewinsexyurl/routers"
)

func init() {
    orm.RegisterDriver("sqlite3", orm.DRSqlite)
    orm.RegisterDataBase("default", "sqlite3", "data.db")
    orm.RunCommand()
}

func main() {
    beego.Run()
}

在主文件中:

package controllers

import (
    "github.com/astaxie/beego"
    "github.com/astaxie/beego/orm"
    "github.com/hobbestigrou/mahewinsexyurl/models"
    _ "github.com/mattn/go-sqlite3"
    "github.com/ventu-io/go-shortid"
    "net/url"
)

type MainController struct {
    beego.Controller
}

type Domain struct {
    Id  int    `form:"-"`
    Url string `form:"url"`
}

func (c *MainController) Get() {
    _ = beego.ReadFromRequest(&c.Controller)

    c.Data["Form"] = &Domain{}
    c.TplName = "index.tpl"
}

func (c *MainController) Post() {
    o := orm.NewOrm()

    flash := beego.NewFlash()
    domain := c.Input().Get("url")

    if _, err := url.ParseRequestURI(domain); err != nil {
        flash.Error("Please put a valid url")
        flash.Store(&c.Controller)
        c.Redirect("/", 302)
    }

    short, _ := shortid.Generate()
    ur := &models.ShortUrl{
        Domain: domain,
        Short:  short,
    }

    if created, _, err := o.ReadOrCreate(&ur, "Domain"); err != nil {
        if created {
            flash.Notice("The url was added: ", short)
            flash.Store(&c.Controller)
            c.Redirect("/", 302)
            } else {
                flash.Notice("The url already exists", short)
                flash.Store(&c.Controller)
                c.Redirect("/", 302)
            }
        }

        c.TplName = "index.tpl"
}

在控制器文件中:

package routers

import (
    "github.com/hobbestigrou/mahewinsexyurl/controllers"
    "github.com/astaxie/beego"
)

func init() {
    beego.Router("/", &controllers.MainController{})
}

我知道我可以重构闪光部分,但目前这是一个细节。

路由器:

./mahewinsexyurl orm syncdb

我使用syncdb创建表:

2016/08/05 17:16:24 [router.go:854][C] the request url is  /
2016/08/05 17:16:24 [router.go:855][C] Handler crashed with error <Ormer> table: `.` not found, maybe not RegisterModel
2016/08/05 17:16:24 [router.go:861][C] /usr/lib/go-1.6/src/runtime/asm_amd64.s:472
2016/08/05 17:16:24 [router.go:861][C] /usr/lib/go-1.6/src/runtime/panic.go:443
2016/08/05 17:16:24 [router.go:861][C] /home/hobbestigrou/go/src/github.com/astaxie/beego/orm/orm.go:110
2016/08/05 17:16:24 [router.go:861][C] /home/hobbestigrou/go/src/github.com/astaxie/beego/orm/orm.go:135
2016/08/05 17:16:24 [router.go:861][C] /home/hobbestigrou/go/src/github.com/hobbestigrou/mahewinsexyurl/controllers/default.go:47
2016/08/05 17:16:24 [router.go:861][C] /home/hobbestigrou/go/src/github.com/astaxie/beego/router.go:763
2016/08/05 17:16:24 [router.go:861][C] /usr/lib/go-1.6/src/net/http/server.go:2081
2016/08/05 17:16:24 [router.go:861][C] /usr/lib/go-1.6/src/net/http/server.go:1472
2016/08/05 17:16:24 [router.go:861][C] /usr/lib/go-1.6/src/runtime/asm_amd64.s:1998

表格已成功创建。但是当我尝试发布数据时,我得到了一个 错误:

Uncaught TypeError: System.registerDynamic is not a function. 

我的错误是什么?

2 个答案:

答案 0 :(得分:3)

我发现我的错误是控制器中的一个粗心错误:

ur := &models.ShortUrl{
    Domain: domain,
    Short:  short,
}

if created, _, err := o.ReadOrCreate(&ur, "Domain"); err == nil {
}

新版本是:

ur := models.ShortUrl{
    Domain: domain,
    Short:  short,
}

if created, _, err := o.ReadOrCreate(&ur, "Domain"); err == nil {
}

错误是&amp;的双重用法工作并创建指向ShortUrl结构的指针。

答案 1 :(得分:0)

我认为你在控制器中遗漏了这个。告诉我们使用哪个数据库。我转到了这个链接:Beego ORM

o := orm.NewOrm()
o.Using("default")
dr = o.Driver()
fmt.Println(dr.Name() == "default") // true
fmt.Println(dr.Type() == orm.DRSqlite) // true

更新尝试

告诉syncdb在命令中使用哪个db:

./mahewinsexyurl orm syncdb -db="default"