1

我只是想用revel,gorm和创建新项目pq。我有Page模型app/models

package models

import (
    "time"
)

type Page struct {
    Id        int64
    Title     string `sql:"size:255"`
    Context   string
    Url       string
    MetaKeys  string
    MetaDescr string
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt time.Time
}

gorm.goapp/controllers

package controllers

import (
    _ "myapp/app/models"
    _ "code.google.com/p/go.crypto/bcrypt"
    _ "database/sql"
    "fmt"
    "github.com/jinzhu/gorm"
    _ "github.com/lib/pq"
    _ "github.com/revel/revel"
    "github.com/revel/revel/modules/db/app"
)

var (
    DB gorm.DB
)

func InitDB() {
    db.Init()

    var err error
    DB, err = gorm.Open("postgres", "user=postgres dbname=mydb_development sslmode=disable")
    if err != nil {
        panic(fmt.Sprintf("Got error when connect database, the error is '%v'", err))
    }

    DB.LogMode(true)

    DB.AutoMigrate(Page{})
}

我有错误undefined: PageDB.AutoMigrate(Page{})但我将我的模型链接在一起_ "myapp/app/models"。怎么了?

4

1 回答 1

2

您忘记添加模型的包标识符:由于您的模型结构是在另一个包中定义的,它的名称(本地controllers包)应该是models.Page.

如果您真的想摆脱包标识符并像在本地定义它一样,我认为您也可以models通过将包分配给.标识符来在本地导入包。例子:

import (
    . "myapp/app/models"
)
于 2014-06-25T13:18:03.273 回答