How to insert multiple rows with array field in a single query?
See original GitHub issueMy 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:
- Created 5 years ago
- Comments:16 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Generic answer to the original question using JSON:
Another generic answer (without
json
functions and withpg-format
):and
node-postgres
should support this.update: not tested, consider this example as pseudo code. but I resolved my issue with this logic.