duankezong4064 2016-03-23 05:14
浏览 71

全选不适用于golaong gorm

I'm using gin framework and trying do crud operation using grom.I'm trying to get data from MYSQL database. i have db.go to get database instances, some controllers to each table and models i have a model like this

    type Campaigns struct {

        ID                     int       `json:"id" form:"id" gorm:"column:CampaignID"`
        UserID                 int       `json:"userId" form:"userId" gorm:"column:UserID"`
        Name                   string    `json:"name" form:"name" gorm:"column:Name"`
        StartDate              time.Time `json:"start" form:"start" gorm:"column:StartDate"`
        EndDate                time.Time `json:"end" form:"end" gorm:"column:EndDate"`
        Customer               string    `json:"customer" form:"customer" gorm:"column:Customer"`
        CustomerID             int       `json:"customerId" form:"customerId" gorm:"column:CustomerID"`
        ImpressionsCounter     int       `json:"ImpressionsCounter" form:"ImpressionsCounter" gorm:"column:ImpressionsCounter"`
        MaxImpressions         int       `json:"maxImpressions" form:"maxImpressions" gorm:"column:MaxImpressions"`
        CurrentSpend           float64   `json:"currentSpend" gorm:"column:CurrentSpend"`
        MaxSpend               float64   `json:"maxSpend" form:"maxSpend" gorm:"column:MaxSpend"`
        Active                 bool      `json:"active" form:"active" gorm:"column:Active"`
        Created                time.Time `json:"created" gorm:"column:DateCreated"`
        Updated                time.Time `json:"updated" gorm:"column:DateCreated"`
}

this is a one controller I'm using

    package controllers
import (
    "time"

  "github.com/op/go-logging"
    "github.com/gin-gonic/gin"
    "github.com/jinzhu/gorm"
  _ "github.com/go-sql-driver/mysql"

    "../models"
)

var log = logging.MustGetLogger("AsAPI")

type AsController struct {
    DB gorm.DB
}

func (ac *AsController) SetDB(d gorm.DB) {
    ac.DB = d
    ac.DB.LogMode(true)
}


// Get all table
func (ac *AsController) ListTable(c *gin.Context) {

    var results []models.Campaigns
  err := ac.DB.Find(&results)

    if err != nil {
        log.Debugf("Error when looking up Table, the error is '%v'", err)
        res := gin.H{
                "status": "404",
                "error": "No Table found",
        }
        c.JSON(404, res)
        return
    }
    content := gin.H{
                        "status": "200",
            "result": "Success",
            "Table": results,
        }

  c.Writer.Header().Set("Content-Type", "application/json")
  c.JSON(200, content)
}

To get database connection I'm using

package controllers
import (
    "time"

  "github.com/op/go-logging"
    "github.com/gin-gonic/gin"
    "github.com/jinzhu/gorm"
  _ "github.com/go-sql-driver/mysql"

    "../models"
)

var log = logging.MustGetLogger("AdsAPI")

type AsController struct {
    DB gorm.DB
}

func (ac *AsController) SetDB(d gorm.DB) {
    ac.DB = d
    ac.DB.LogMode(true)
} 

and I'm using following routs

ac := controllers.AdsController{}
ac.SetDB(dc.GetDB())


// Get a Ads resource
router := gin.Default()

router.GET("/table", ac.ListTables)

when i run this I'm getting following error

(/api/controllers/table.go:30) 
[2016-03-23 09:56:39]  [0.99ms]  SELECT  * FROM `tables`  
2016/03/23 09:56:39 Error when looking up tables, the error is '&{0xc8202140e0 sql: Scan error on column index 3: unsupported driver -> Scan pair: []uint8 -> *time.Time 1 <nil> 0xc82022f860 0xc82022f7c0 0xc82021e140 2 {0xc8201fb4a0} <nil> false  map[] map[]}'
[GIN] 2016/03/23 - 09:56:39 | 404 |    1.153811ms | 127.0.0.1 |   GET     /table

what is the reason for this error ? help me to fix this error ?

  • 写回答

2条回答 默认 最新

  • douchibu7040 2016-03-23 07:48
    关注

    You can find the answer in the driver documentation https://github.com/go-sql-driver/mysql#timetime-support:

    The default internal output type of MySQL DATE and DATETIME values is []byte which allows you to scan the value into a []byte, string or sql.RawBytes variable in your programm.

    However, many want to scan MySQL DATE and DATETIME values into time.Time variables, which is the logical opposite in Go to DATE and DATETIME in MySQL. You can do that by changing the internal output type from []byte to time.Time with the DSN parameter parseTime=true. You can set the default time.Time location with the loc DSN parameter.

    Alternatively you can use the NullTime type as the scan destination, which works with both time.Time and string / []byte.

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?