英文:
GIN does not provide all of the postgres db values in get request
问题
我有一个Postgres数据库,其中有以下条目:
我正在使用GIN,并在我的主函数中有以下代码:
func main() {
router := gin.Default()
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=%s TimeZone=%s", env_var.Host, env_var.User, env_var.Password, env_var.DBname, env_var.Port, env_var.SSLMODE, env_var.TimeZone)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect to db")
}
router.GET("/source/:id", han.GetSource(db))
router.Run("localhost:8080")
}
以下是结构体和处理程序:
func GetSource(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("id")
var source models.Source
db.Where("id = ?", id).Find(&source)
c.JSON(http.StatusOK, source)
}
}
type Source struct {
ID int `json:"id"`
Source_Name string `json:"source_name"`
Abbreviation string `json:"abbreviation"`
Last_Modified time.Time `json:"last_modified"`
}
func (Source) TableName() string {
return "artwork_migrate_source"
}
当我使用以下curl命令时:
curl localhost:8080/source/1
我得到以下响应:
{"id":1,"source_name":"Rijksmuseum","abbreviation":"","last_modified":"2022-08-05T00:00:00Z"}%
到目前为止一切正常,但是如果你注意到,"abbreviation"字段是空的。我的数据库中的该字段是varchar类型,限制为3个字符。
英文:
I have a Postgres DB which has the following entry
I'm using GIN and have the following in my main function
func main() {
router := gin.Default()
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=%s TimeZone=%s", env_var.Host, env_var.User, env_var.Password, env_var.DBname, env_var.Port, env_var.SSLMODE, env_var.TimeZone)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect to db")
}
router.GET("/source/:id", han.GetSource(db))
router.Run("localhost:8080")
}
Here are the structs and handlers
func GetSource(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("id")
var source models.Source
db.Where("id = ?", id).Find(&source)
c.JSON(http.StatusOK, source)
}
}
type Source struct {
ID int `json:"id"`
Source_Name string `json:"source_name"`
Abrviation string `json:"abrviation"`
Last_Modified time.Time `json:"last_modified"`
}
func (Source) TableName() string {
return "artwork_migrate_source"
}
When I use the following curl command
curl localhost:8080/source/1
I get the following response:
{"id":1,"source_name":"Rijksmuseum","abrviation":"","last_modified":"2022-08-05T00:00:00Z"}%
So far so good, but if you notice, the "abbreviation" field is empty. The field in my db is of type varchar with a limit of 3.
答案1
得分: 1
简单的错误,我在结构体中拼错了"Abbreviation"字段。
感谢@Brits的帮助。
英文:
Simple mistake, I misspelled the "Abriviation" field in the struct.
Thanks to @Brits
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论