Postgres函数填充记录集和返回的ID

时间:2017-11-03 20:30:57

标签: sql database postgresql insert plpgsql

我正在尝试INSERT记录,返回ID,然后将ID传递给其他表并插入其他数据。要插入的数据采用JSON格式。

我正在使用函数调用来插入数据,并且当create语句有效时,我在执行期间做错了(所以要么a)我没有正确地写出语句或b)我没有正确传递数据。

我得到的错误是这样的:

ERROR: INSERT has more target columns than expressions
SQL state: 42601
Context: PL/pgSQL function insert_to_tables(jsonb,jsonb,jsonb) line 3 at SQL statement

以下是我的表格外观:

CREATE TABLE main_data(
  id SERIAL PRIMARY KEY,
  field_1 TEXT,
  some_time DATE
);

CREATE TABLE locale_data(
  locale_id SERIAL PRIMARY KEY,
  city TEXT,
  state TEXT,
  address TEXT,
  main_data_id INTEGER REFERENCES main_data(id)
);

CREATE TABLE demographic_data(
  demographic_id SERIAL PRIMARY KEY,
  age INT,
  ethnicity TEXT,
  main_data_id INTEGER REFERENCES main_data(id)
);

我有一个像这样的函数来处理数据输入和解析:

CREATE OR REPLACE FUNCTION insert_to_tables (
main_data_fields JSONB,
locale_data_fields JSONB,
demographic_data_fields JSONB,
OUT new_user_id INTEGER
)
RETURNS integer AS $$
BEGIN
   WITH ins AS (
      INSERT INTO main_data SELECT * FROM jsonb_populate_recordset(NULL::main_data, $1::jsonb)
      RETURNING id
      )

   INSERT INTO locale_data(city, state, address, main_data_id)
   SELECT i.id AS main_data_id, jsonb_populate_recordset(NULL::locale_data, $2::jsonb)
   FROM  ins i;
   INSERT INTO demographic_data(age, ethnicity)
   SELECT i.id AS main_data_id, jsonb_populate_recordset(NULL::demographic_data, $3::jsonb)
   FROM  ins i;
END;
$$ LANGUAGE plpgsql;

要插入数据,我调用的函数如下:

select insert_to_tables(
'{"field_1": "http://www.google.com", "some_time": "09-02-2019"}',
'[{"city": "a city", "address": "123 fake road", "state": "CA"}, {"city": "little city", "address": "456 noname road", "state": "WA"}]',
'[{"age": 45, "ethnicity": "Asian"}, {"age": "45", "ethnicity": "Egyptian"}]'
);

我的预期输出应该是填充了1行的main_data表,填充了2行的locale_data表,以及填充了2行的demographic_data表。

localedemographic表中的每一行都属于并且应该引用main_data表中的行,如下所示:

id  |         field1_1        | some_time  |
----+-------------------------+------------+
 1  |  http://www.google.com  |  09-02-2019


locale_id  |    city        |  state  |     address      | main_data_id
-----------+----------------+---------+------------------+---------+
 1         |     a city     |   CA    | 123 fake road    |   1
 2         |   little city  |   WA    | 456 noname road  |   1

locale_id  |    age      |    ethnicity   |  main_data_id
-----------+-------------+----------------+------------------+
 1         |     45      |   Asian        |    1
 2         |     45      |   Egyptian     |    1 

我想我的第二组INSERT INTO语句是错误的,但我不确定如何在从另一个操作返回ID后处理JSON的数据插入。

1 个答案:

答案 0 :(得分:1)

主要问题 - 使用jsonb_populate_recordset()时,必须确保json值的数量与表列的数量完全相同。您不希望填充id表,因为它们是串行的,因此您必须指明列列表和相应的选择列表。另外,函数jsonb_populate_recordset()适用于json数组,因此第一个参数必须是一个数组,就像其他两个一样。

公用表expresion(带命令)是一个包含多个子查询的查询,因此函数语言应为SQL,最终查询应返回插入id的行的main_data。 / p>

CREATE OR REPLACE FUNCTION insert_to_tables (
    main_data_fields JSONB,
    locale_data_fields JSONB,
    demographic_data_fields JSONB
)
RETURNS integer AS $$
    WITH ins AS (
        INSERT INTO main_data (field_1, some_time)
        SELECT field_1, some_time
        FROM jsonb_populate_recordset(NULL::main_data, $1::jsonb)
        RETURNING id
    ),
    ins_locale AS (
        INSERT INTO locale_data (city, state, address, main_data_id)
        SELECT city, state, address, ins.id
        FROM ins, jsonb_populate_recordset(NULL::locale_data, $2::jsonb)
    ),
    ins_demographic AS (
        INSERT INTO demographic_data (age, ethnicity, main_data_id)
        SELECT age, ethnicity, ins.id
        FROM ins, jsonb_populate_recordset(NULL::demographic_data, $3::jsonb)
    )
    SELECT id
    FROM ins;
$$ LANGUAGE sql;

SELECT insert_to_tables (
    '[{"field_1": "http://www.google.com", "some_time": "09-02-2019"}]',
    '[{"city": "a city", "address": "123 fake road", "state": "CA"}, {"city": "little city", "address": "456 noname road", "state": "WA"}]',
    '[{"age": 45, "ethnicity": "Asian"}, {"age": "45", "ethnicity": "Egyptian"}]'
);

See the full example here.