doupu2722 2019-04-29 04:43
浏览 65

去狂欢未定义:sql或Txn

mac highsierra use mysql

https://paiza.hatenablog.com/entry/2018/03/23/paizacloud_golang_revel

I'm trying to use the Revel framework for mysql. It's revel Tutorial booking.

what is error?

ERROR 18:33:28 watcher.go:270: Build detected an error                  error="Go Compilation Error (in app/controllers/gorm.go:31): undefined: sql"

before

ERROR 18:18:26 watcher.go:270: Build detected an error                  error="Go Compilation Error (in app/controllers/app.go:26): c.Txn undefined (type Application has no field or method Txn)" 

I did this because I got an error before

controllers/gorm.go

type Transactional struct {
    *revel.Controller
    Txn *sql.Tx
}

After running it results in error Please tell me how you can solve it

add controllers/app.go

package controllers

import (
    "github.com/revel/revel"
    "booking/app/models"
    "booking/app/routes"
    "database/sql"
)
type Application struct {
    *revel.Controller
}

func (c Application) Index() revel.Result {
    if c.connected() != nil {
        return c.Redirect(routes.Hotels.Index())
    }
    c.Flash.Error("Please log in first")
    return c.Render()
}

func (c Application) connected() *models.User {
    if c.ViewArgs["user"] != nil {
        return c.ViewArgs["user"].(*models.User)
    }
    if username, ok := c.Session["user"]; ok {
        return c.getUser(username.(string))
    }

    return nil
}

controllers/gorm.go

package controllers

import (
    _ "github.com/go-sql-driver/mysql"
    "github.com/jinzhu/gorm"
    "github.com/revel/revel"
    "booking/app/models"
    "log"
    )


type GormController struct {
    *revel.Controller
    Txn *gorm.DB
}
func InitDB() {
    dbInfo, _ := revel.Config.String("db.info")
    db, err := gorm.Open("mysql", dbInfo)
    if err != nil {
        log.Panicf("Failed gorm.Open: %v
", err)
    }

    db.DB()
    db.AutoMigrate(&models.Booking{})
    db.AutoMigrate(&models.Hotel{})
    db.AutoMigrate(&models.User{})

    DB = db
}


func (c *GormController) SetDB() revel.Result {
    c.Txn = DB
    return nil
}

type Transactional struct {
    *revel.Controller
    Txn *sql.Tx
}

tree enter image description here maybe import miss?

  • 写回答

1条回答 默认 最新

  • duandaoji3992 2019-04-30 02:47
    关注

    edit app.go file

    package controllers
    
    import (
        "github.com/revel/revel"
        "booking/app/models"
        "booking/app/routes"
        "database/sql"
    )
    
    type Application struct {
        *Gorm.Controller
    }
    

    edit gorm.go file

    package controllers
    
    import (
        _ "github.com/go-sql-driver/mysql"
        "github.com/jinzhu/gorm"
        "github.com/revel/revel"
        "booking/app/models"
        "database/sql"
        "log"
        )
    
    
    type GormController struct {
        *revel.Controller
        Txn *gorm.DB
    }
    

    But App have error.

        revel run booking
    Revel executing: run a Revel application
    WARN  11:35:42    run.go:150: No http.addr specified in the app.conf listening on localhost interface only. This will not allow external access to your application 
    ERROR 11:35:42 reflect.go:176: Error: Failed to find import path for    package=Gorm type=Controller 
    WARN  11:35:42 source_info.go:113: Type found in package: controllers, but did not embed from: revel.Controller foundstructures=GormController,Transactional,Static,TestRunner,  name=Hotels importpath=booking/app/controllers
    WARN  11:35:42 source_info.go:113: Type found in package: controllers, but did not embed from: revel.Controller name=Application importpath=booking/app/controllers foundstructures=GormController,Transactional,Static,TestRunner, 
    /Users/toshi/gocode/src/booking/app/controllers/app.go
    ERROR 11:35:43 watcher.go:270: Build detected an error                  error="Go Compilation Error (in app/controllers/app.go:11): undefined: Gorm" 
    

    Please tell me how you can solve it

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!