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

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

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

问题

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

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

我正在使用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

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

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

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:

确定