dsp15140275697 2019-07-26 20:49
浏览 28
已采纳

RETURNING必须包含至少一个值

I'm learning to use PostgreSQL and GORM. Trouble came relatively quickly, and it feels as if I'm the only one having this problem ever.

Here, I'm creating a struct, instantiating it, and trying to write it to database. However, it returns an error:

pq: RETURNING must contain at least one value

Tried googling it, and the only thing I found is Postgres source code which might as well be in Chinese.

Source code:

db, err := gorm.Open("postgres", "user=postgres dbname=testdb sslmode=disable password=qwerty")
if err != nil {
    panic(err.Error())
}
defer db.Close()

database := db.DB()

err = database.Ping()
if err != nil {
    panic(err.Error())
}

db.AutoMigrate(&Currency{})

fmt.Println("Connection to PostgreSQL was successful!")

testCur := Currency{"shekels", 20}
if db.NewRecord(testCur) {
    err := db.Create(&testCur).Error
    if err != nil {
        panic(err.Error())
    }
}

Struct:

type Currency struct {
name string
rate uint
}

And yes, it must be done with Postgres and GORM. Database is brand new, empty.

  • 写回答

1条回答 默认 最新

  • doukun5339 2019-07-26 21:17
    关注

    One thing that stands out is that your struct only contains non-exported fields:

    type Currency struct {
        name string
        rate uint
    }
    

    Neither of those fields will be visible to Gorm (which presumably uses reflection to convert your struct to SQL). That would lead to Gorm trying to do an empty INSERT with a RETURNING clause to get something back but, since Gorm can't see anything to INSERT it won't be asking for anything in the RETURNING clause and there's your rather confusing error.

    I think you'll have better luck if you fix your struct to export those fields:

    type Currency struct {
        Name string
        Rate uint
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)