json - Postgresql - 在从 JSON 和表中选择值时插入到两个表中

标签 json postgresql insert

需要一些想法。

我正在尝试从我的 nodejs 应用程序将值插入到两个表中,我想同时将数据插入到两个表中,因为我使用第一个表的 ID 作为第二个表列中的父 ID。 这是表结构

表1

                                  Table “public.table1”
   Column    |     Type      | Collation | Nullable |                Default
-------------+---------------+-----------+----------+---------------------------------------
 id          | integer       |           | not null | nextval(‘table1_id_seq’::regclass)
 name        | text          |           | not null |
 description | character(50) |           |          |
Indexes:
    “table1_pkey” PRIMARY KEY, btree (id)

表 2:

                                  Table “public.table2”
   Column    |     Type      | Collation | Nullable |              Default
-------------+---------------+-----------+----------+-----------------------------------
 id          | integer       |           | not null | nextval(‘table2_id_seq’::regclass)
 name        | text          |           | not null |
 parent_id   | text          |           | not null |
 sequence    | integer       |           | not null |
 description | character(50) |           |          |

我正在从 UI 获取 JSON 格式的值

{“table1”:{“name”:“test”,“description”:“test123”,“table2”:[{“column1”:“WMS”,“column2”:“WMS”,“column3”:2,“column4":“rtest”}]}}

我写了下面的查询但是得到了以下错误:

WITH new_table1 AS(
  INSERT INTO TABLE1
      (id, name, description)
    VALUES
      (nextval('table_sequence'), 'BDO', 'Sample test') returning id
) INSERT INTO TABLE2(id,parent_id,name,sequence,description)  (  
  nextval('table2_sequence'), 
  (select id from new_table1), 
  (select column1, column3, column4
from jsonb_to_recordset(
    '[{"column1":"WMS","column2":"WMS","column3":2,"column4":"rtest","icon":"sap-icon://it-host"}]'
) r (column1 text, column2 text,column3 int, column4 text, icon text)) );

错误:

r (column1 text, column2 text,column3 int, column4 text, icon text)) );
ERROR:  subquery must return only one column
LINE 9:   (select column1, column3
      ^

最佳答案

错误的来源是您只能在 VALUES 列表中使用标量表达式(返回单个列的子查询)(您也错过了该关键字)。你可以这样解决这个问题:

WITH new_table1 AS (
  INSERT INTO table1 (name, description)
  VALUES ('BDO', 'Sample test') RETURNING id )
INSERT INTO table2 (parent_id, name, sequence, description)   
  SELECT id, column1, column3, column4
  FROM new_table1,
       jsonb_to_recordset (
         '[{"column1":"WMS","column2":"WMS","column3":2,"column4":"rtest","icon":"sap-icon://it-host"}]'
       ) r (column1 text, column2 text,column3 int, column4 text, icon text);

解决方案在于使用带有 INSERT 语句的查询,而不是 VALUES 子句。在查询中,您将 table1 上的 INSERT 语句与 jsonb_to_recordset() 的输出相结合。这是笛卡尔积,但由于 INSERT 只生成一行,所以没有问题。

另请注意,我已经删除了对 id 列及其序列的引用 - 默认值的优点在于您不必在插入时指定任何...

关于json - Postgresql - 在从 JSON 和表中选择值时插入到两个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51792691/

相关文章:

json - ML 模型输入和输出类比

javascript - 使用 AJAX 加载 GZIP JSON 文件

postgresql - 从 PostgreSQL 导出的编码问题

sql - 错误: syntax error at or near "and"

Mysql 插入变量

database - 为什么我的经典 ASP 代码会插入重复记录(即两条记录而不是一条记录)?

node.js - Node js访问json结果中的值并获取该值

php - JavaScript 和 JSON

python - 使用 UPDATE ... RETURNING 的多个数据库连接,似乎不更新任务表中的行

java - 在 MySQL 中插入多行