doubi2014 2014-06-25 13:09
浏览 42
已采纳

陶醉于Gorm“未定义:页面”

I'm just trying to create new project with revel, gorm and pq. I have model Page in 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
}

and gorm.go in app/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{})
}

I have the error undefined: Page in line DB.AutoMigrate(Page{}), but I linked my models in line _ "myapp/app/models". What's wrong?

  • 写回答

1条回答 默认 最新

  • duanfeng3879 2014-06-25 13:18
    关注

    You forgot to add the package identifier of your model: as your model struct is defined in another package, it's name (locally to the controllers package) should be models.Page.

    If you really want to get rid of the package identifier and do like it was locally defined, I think that you can also import your models package locally by assigning it to the . identifier. Example:

    import (
        . "myapp/app/models"
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line