GIN在GET请求中没有提供所有的PostgreSQL数据库值。

huangapple go评论125阅读模式
英文:

GIN does not provide all of the postgres db values in get request

问题

我有一个Postgres数据库,其中有以下条目:

GIN在GET请求中没有提供所有的PostgreSQL数据库值。

我正在使用GIN,并在我的主函数中有以下代码:

  1. func main() {
  2. router := gin.Default()
  3. 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)
  4. db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
  5. if err != nil {
  6. panic("failed to connect to db")
  7. }
  8. router.GET("/source/:id", han.GetSource(db))
  9. router.Run("localhost:8080")
  10. }

以下是结构体和处理程序:

  1. func GetSource(db *gorm.DB) gin.HandlerFunc {
  2. return func(c *gin.Context) {
  3. id := c.Param("id")
  4. var source models.Source
  5. db.Where("id = ?", id).Find(&source)
  6. c.JSON(http.StatusOK, source)
  7. }
  8. }
  9. type Source struct {
  10. ID int `json:"id"`
  11. Source_Name string `json:"source_name"`
  12. Abbreviation string `json:"abbreviation"`
  13. Last_Modified time.Time `json:"last_modified"`
  14. }
  15. func (Source) TableName() string {
  16. return "artwork_migrate_source"
  17. }

当我使用以下curl命令时:

  1. curl localhost:8080/source/1

我得到以下响应:

  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

GIN在GET请求中没有提供所有的PostgreSQL数据库值。

I'm using GIN and have the following in my main function

  1. func main() {
  2. router := gin.Default()
  3. 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)
  4. db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
  5. if err != nil {
  6. panic("failed to connect to db")
  7. }
  8. router.GET("/source/:id", han.GetSource(db))
  9. router.Run("localhost:8080")
  10. }

Here are the structs and handlers

  1. func GetSource(db *gorm.DB) gin.HandlerFunc {
  2. return func(c *gin.Context) {
  3. id := c.Param("id")
  4. var source models.Source
  5. db.Where("id = ?", id).Find(&source)
  6. c.JSON(http.StatusOK, source)
  7. }
  8. }
  9. type Source struct {
  10. ID int `json:"id"`
  11. Source_Name string `json:"source_name"`
  12. Abrviation string `json:"abrviation"`
  13. Last_Modified time.Time `json:"last_modified"`
  14. }
  15. func (Source) TableName() string {
  16. return "artwork_migrate_source"
  17. }

When I use the following curl command

  1. curl localhost:8080/source/1

I get the following response:

  1. {"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

huangapple
  • 本文由 发表于 2022年8月6日 06:57:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/73255965.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定