duanhanzi8328 2018-07-20 17:29
浏览 29
已采纳

不支持的型号* settingsmodel.Settings?

I'm using this ORM library for PostgreSQL: https://godoc.org/github.com/go-pg/pg#example-DB-Select and I'm having an odd issue which I don't understand.

I'm trying to SELECT data from my settings table to later update that value with a function.

package settingsmodel

import (
    . "database"
)

type Settings struct {
    Id int64
    SiteName string
}

func Set(newValue string) bool {
    site := &Settings {
        SiteName: "MySite",
    }

    err := Db.Select(&site)
    if err != nil {
        panic(err) // This is where it panics
    }

    site.SiteName = newValue
    err = Db.Update(site)
    if err != nil {
        panic(err)
    }

    return true
}

The error I'm getting is panic: pg: Model(unsupported *settingsmodel.Settings)

I have another function (in the same file) where I get the site name and it works perfectly fine:

func Get() string {
    var site Settings

    err := Db.Model(&site).First()
    if err != nil {
        panic(err)
    }

    return site.SiteName
}

I really don't understand why it's not working. Any help? Thanks!

  • 写回答

1条回答 默认 最新

  • dousui4577 2018-07-20 17:36
    关注

    In the working example, you're passing a pointer to settings; in the first (non-working) example, you're passing a pointer to a pointer:

    // &Settings - site is a pointer to a Settings struct
    site := &Settings {
        SiteName: "MySite",
    }
    
    // &site - pass a pointer to site, which is already a pointer
    err := Db.Select(&site)
    

    vs the working one:

    // site is a value, not a pointer
    var site Settings
    
    // Pass a pointer to the value
    err := Db.Model(&site).First()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏