我有像 blow 这样的 json 字段,我想将其存储在数据库中
{
id: 1
name: "test entity 1"
description: "a test entity for some guy's blog"
status: "passed"
web_url: "http://localhost:3000"
jobs: [{
id: "1"
name: "test1"
status: "passed"
},
{
id: "2"
name: "test2"
status: "passed"
},
{
id: "3"
name: "test3"
status: "failed"
}]
}
我继续使用一种方法来创建表用途:
CREATE TABLE test3 (id INT PRIMARY KEY, name VARCHAR, description VARCHAR, status VARCHAR, web_url VARCHAR, jobs JSON[]);
和插入数据使用:
sqlStatement := `
INSERT INTO jobs (id, name, description, status, web_url, jobs)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (id) DO UPDATE
SET status = $4
RETURNING id`
id := 0
err = database.Db.QueryRow(sqlStatement, y[i].ID, y[i].Name, y[i].Description, y[i].Status, y[i].WebURL, jobsdata).Scan(&id)
if err != nil {
panic(err)
}
但不会工作,需要帮助!
获取错误:
panic: sql: converting argument $6 type: unsupported type handler.Jobs, a slice of struct
我想要的是:
postgres=# SELECT * FROM test3;
id | name | description | status | web_url | jobs
------+------------------------------------------+--------+---------+----------------------------------------------------------+----------------------------------------------------------
1 | test entity 1 | a test entity for some guy's blog | passed | https://localhost:3000 | {id: "1",name: "test1", status: "passed"},{id: "2",name: "test2", status: "passed"},{id: "3",name: "test3", status: "failed"}
LEATH
蛊毒传说
相关分类