给定一个包含 type 列的表jsonb[]
,如何将 json 数组插入到列中?
使用提供的格式化程序:array
, :json
在这种情况下不起作用 - 除非我错过了正确的组合或其他东西。
const links = [
{
title: 'IMDB',
url: 'https://www.imdb.com/title/tt0076759'
},
{
title: 'Rotten Tomatoes',
url: 'https://www.rottentomatoes.com/m/star_wars'
}
];
const result = await db.none(`INSERT INTO tests (links) VALUES ($1:json)`, [links]);
你不需要图书馆的:json
在这种情况下进行过滤,因为您需要一个 JSON 对象数组,而不是带有 JSON 对象数组的 JSON。
前者默认格式正确,后者只需要::json[]
类型铸造:
await db.none(`INSERT INTO tests(links) VALUES($1::json[])`, [links]);
其他注意事项
- Use PG-监视器 https://github.com/vitaly-t/pg-monitor或事件query https://vitaly-t.github.io/pg-promise/global.html#event:query输出正在执行的查询,以便于诊断。
- Method none https://vitaly-t.github.io/pg-promise/Database.html#none只能解决
null
,没有必要将结果存储在变量中。
- Library
pg-promise
没有任何:array
过滤器,参见支持的过滤器 https://github.com/vitaly-t/pg-promise#formatting-filters.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)