我正在尝试使用jackc/pgx在PostgreSQL中插入/更新数据到一个包含自定义类型列的表中。这是一个golan结构体编写的表类型:
// Added this struct as a Types in PSQL
type DayPriceModel struct {
Date time.Time `json:"date"`
High float32 `json:"high"`
Low float32 `json:"low"`
Open float32 `json:"open"`
Close float32 `json:"close"`
}
// The 2 columns in my table
type SecuritiesPriceHistoryModel struct {
Symbol string `json:"symbol"`
History []DayPriceModel `json:"history"`
}
我已经写了下面的代码:
func insertToDB(data SecuritiesPriceHistoryModel) {
DBConnection := config.DBConnection
_, err := DBConnection.Exec(context.Background(), "INSERT INTO equity.securities_price_history (symbol) VALUES ($1)", data.Symbol, data.History)
}
但我无法插入自定义数据类型(DayPriceModel
)。
我得到了一个类似failed to encode args[1]: unable to encode
的错误(错误很长,主要显示我的数据,所以我选择了主要部分。
如何将具有这种自定义数据类型的数据INSERT到PSQL中?
PS:使用jackc/pgx的实现是首选,但数据库/sql也可以
1条答案
按热度按时间e0bqpujr1#
我对pgx不够熟悉,不知道如何设置对复合类型数组的支持,但是,正如注解中提到的,你可以实现
driver.Valuer
接口,并让它产生一个有效的文本,这也适用于你存储结构体切片的情况,你只需要声明一个命名切片,并让它实现赋值器,然后使用它来代替未命名切片。在编写插入查询时,请确保在占位符之后添加显式强制转换,例如。
注意#1:根据postgres中复合类型的确切定义,上面的例子可能会起作用,也可能不会起作用,如果不起作用,只需调整代码使其起作用。
注意#2:上面例子中的一般方法是有效的,但是它可能不是非常有效。如果你需要代码是高性能的,不要逐字使用例子。