question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to insert multiple rows with array field in a single query?

See original GitHub issue

My table fields are like that: id: pk integer name: text info: integer[] I am gonna insert multiple rows in a single query.

client.query(
    "INSERT INTO mytable (name, info) SELECT * FROM UNNEST ($1::text[], $2::int[])",
    [
         ["John", "Adam", "Mark"],
         [[1,2,3,4], [2,3,5,4],[4,4,5,8]]
    ]
)

But I got the error: error: column "info" is of type integer[] but expression is of type integer. How can I do this?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

12reactions
charmandercommented, Mar 5, 2020

Generic answer to the original question using JSON:

client.query(
    `INSERT INTO mytable (name, info)
     SELECT name, info FROM jsonb_to_recordset($1::jsonb) AS t (name text, info int[])`,
    [
        JSON.stringify([
            {name: "John", info: [1, 2, 3, 4]},
            {name: "Adam", info: [2, 3, 5, 4]},
            {name: "Mark", info: [4, 4, 5, 8]},
        ]),
    ]
)
2reactions
muratgozelcommented, Oct 12, 2021

Another generic answer (without json functions and with pg-format):

const pgformat = require('pg-format');

client.query(`
  INSERT INTO mytable (name, info)
  SELECT name::text, info::int[] FROM UNNEST($1::text, $2::int[]) AS t (name, info)`,
  [
    ["John", '{' + pgformat('%I', [1, 2, 3, 4]]) + '}'],
    ["Adam", '{' + pgformat('%I', [2, 3, 5, 4]]) + '}'],
    ["Mark", '{' + pgformat('%I', [4, 4, 5, 8]]) + '}'],
  ]
)

and node-postgres should support this.

update: not tested, consider this example as pseudo code. but I resolved my issue with this logic.

Read more comments on GitHub >

github_iconTop Results From Across the Web

inserting multiple rows in one query from an array [duplicate]
$sql = "INSERT INTO staff (name, age, address) VALUES "; $values = array(); for ($i = 1; $i <= $count; $i++) { $values[]...
Read more >
How to insert multiple rows and columns in database using array
$sql = "INSERT INTO myguest (array(firstname => 'john'));. php · mysql · array · Share.
Read more >
How to Insert Multiple Rows in SQL - AppDividend
To insert multiple rows in SQL, use an INSERT Statement. Alternative approaches are insert into select statement and Union All keyword.
Read more >
How to INSERT Multiple Records in SQL - DigitalOcean
SQL INSERT query inserts data into the columns of a particular table. The normal SQL INSERT query inputs the data values in a...
Read more >
SQL Query to Insert Multiple Rows - GeeksforGeeks
SQL Query to Insert Multiple Rows ... Insertion in a table is a DML (Data manipulation language) operation in SQL. When we want...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found