dqpciw9742 2016-08-05 16:06
浏览 210
已采纳

beego模型错误

I'm quite new on beego. I start a little project to discover and start to learn the framework. I used the bee command to create the project:

bee new my_project

The version of beego is:

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

I have a little model, the content of my model file is:

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))
}

In the main file:

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()
}

In the controller file:

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"
}

I know I can refactor the flash part but it's a detail at the moment.

The router:

package routers

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

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

I used the syncdb to create the table:

./mahewinsexyurl orm syncdb

The table was successfully created. But when I try to post data, I got an error:

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

What is my mistake ?

  • 写回答

2条回答 默认 最新

  • dongyunwei8596 2016-08-06 17:16
    关注

    I found my error it was a careless mistake in the controllers:

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

    Thew new version is:

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

    The error is the double usage of & to work and create pointer to a ShortUrl struct.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图