在Go应用程序中,我有一个名为entries
的数组。我正在尝试解析该数组,并将带有json对象的数组插入到PostgreSQL数据库的表中(在我的情况下是Details
数组)。如何正确制作?
type Details struct {
Year int `json:"year"`
Month int `json:"month"`
}
type Entry struct {
ID int `json:"id"`
Details []Details `json:"details"`
}
var entries = []Entry{
{
ID: 107509116290,
Details: []Details{
{
Year: 2018,
Month: 6
},
{
Year: 2018,
Month: 7
}
}
},
{
ID: 107509116280,
Details: []Details{
{
Year: 2019,
Month: 7
},
{
Year: 2019,
Month: 8
}
}
}
}
err = database.DBGORM.Exec("CREATE TABLE IF NOT EXISTS layers (ID NUMERIC, DETAILS JSON);").Error; if err != nil {
fmt.Println(err)
return
}
for i := 0; i < len(entries); i++ {
err = database.DBGORM.Exec("INSERT INTO layers (ID, DETAILS) VALUES ($1, $2)", entries[i].ID, entries[i].Details).Error; if err != nil {
fmt.Println(err)
return
}
}
现在,当尝试测试上面的代码时,我遇到了这样的错误:
sql: converting argument $2 type: unsupported type controllers.Details, a struct