beego模型已注册但未找到。

huangapple go评论81阅读模式
英文:

beego models registered but not found

问题

我正在尝试编写我的第一个beego web应用程序,并尝试注册一些模型。我在models/model.goinit()函数中注册它们,但是当我运行bee run命令时,出现以下错误:
no Model found, need register your model

main.go:

package main
import (
    "fmt"
    _ "test_blog/routers"
    "time"

    _ "github.com/lib/pq"

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

func init() {
    orm.RegisterDriver("postgres", orm.DRPostgres)

    maxIdle := 30
    maxConn := 30
    orm.RegisterDataBase(
        "default",
        "postgres", 
        "postgres://user:password@localhost/test_db", maxIdle, maxConn
    )

    orm.DefaultTimeLoc = time.UTC
}

func main() {
    // Database alias.
    name := "default"

    // Drop table and re-create.
    force := false

    // Print log.
    verbose := false

    // Error.
    err := orm.RunSyncdb(name, force, verbose)
    if err != nil {
        fmt.Println(err)
    }

    beego.Run()
}

注意: 在第一次运行bee run之前,forceverbose都被设置为true

models.go:

package main

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

type User struct {
    Name  string
    Posts []*Post `orm:"reverse(many)"`
}

type Post struct {
    Title   string    `orm:"size(50)"`
    Text    string    `orm:"size(4000)"`
    Created time.Time `orm:"auto_now_add;type(datetime)"`
    Updated time.Time `orm:"auto_now;type(datetime)"`
    Author  *User     `orm:"rel(fk)"`
}

func init() {
    orm.RegisterModel(new(User), new(Post))
}
英文:

I'm trying to write my first beego web app and I'm trying to register some models, i register them in models/model.go's init() function, but when I run the command bee run, I get the following error:
no Model found, need register your model

main.go:

package main
import (
   "fmt"
	_ "test_blog/routers"
   "time"

   _ "github.com/lib/pq"

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

func init() {
    orm.RegisterDriver("postgres", orm.DRPostgres)

    maxIdle := 30
	maxConn := 30
    orm.RegisterDataBase(
        "default",
        "postgres", 
        "postgres://user:password@localhost/test_db", maxIdle, maxConn
    )

    orm.DefaultTimeLoc = time.UTC
}

func main() {
    // Database alias.
	name := "default"

	// Drop table and re-create.
	force := false

    // Print log.
	verbose := false

	// Error.
	err := orm.RunSyncdb(name, force, verbose)
	if err != nil {
		fmt.Println(err)
	}

    beego.Run()
}

Note: force & verbose both were set to true before running bee run for the first time.

models.go:

package main

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

type User struct {
    Name  string
    Posts []*Post `orm:"reverse(many)"`
}

type Post struct {
	Title   string    `orm:"size(50)"`
	Text    string    `orm:"size(4000)"`
    Created time.Time `orm:"auto_now_add;type(datetime)"`
	Updated time.Time `orm:"auto_now;type(datetime)"`
    Author  *User     `orm:"rel(fk)"`
}

func init() {
    orm.RegisterModel(new(User), new(Post))
}

答案1

得分: 4

请尝试以下操作:

  1. 在models.go中,将package main改为package models
  2. 在main.go中,添加import _ "test_blog/models"
英文:

try this:

  1. in models.go, change package main to package models
  2. in main.go, add import _ "test_blog/models"

huangapple
  • 本文由 发表于 2017年3月7日 06:58:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/42637163.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定