如何使用 knex/postgres 创建可以存储对象数组的列

How to create a column that can store an array of objects using knex/postgres

我想在 table 上存储一个简单的数组。该数组应包含对象。

我找不到任何关于如何在线创建此专栏的示例...我看到也许是 jsonb?

不过我在文档中没有看到数组选项...

http://knexjs.org/#Schema-jsonb

我正在使用 knex + postgres + node.js

Knex 有 2 种方法可以将 json 存储在关系数据库中。

json 用于以人类可读的方式存储 json。

jsonb,二进制存储方式

For PostgreSQL, due to incompatibility between native array and json types, when setting an array (or a value that could be an array) as the value of a json or jsonb column, you should use JSON.stringify() to convert your value to a string prior to passing it to the query builder, e.g.

knex.table('users')
  .where({id: 1})
  .update({json_data: JSON.stringify(mightBeAnArray)});