How to insert multiple rows when one of the columns is type of array #302
Replies: 1 comment
-
|
Well, I also searched the issues of First, you need to serialize the array value into its PostgreSQL format. So const pgformat = require('pg-format')
const arrliteral = '{' + pgformat('%I', ['en_US']) + '}'Second, you need to define columns along with their types inside the await pgconn.query(sql`
insert into project (uuid, title, email_service, supported_locales)
select uuid::text, title::text, email_service::jsonb, supported_locales::text[]
from ${selectQuery}
as t (uuid, title, email_service, supported_locales)
returning *
`)So the test will look like this at final: (async () => {
const {createPool, sql} = require('slonik')
const pgformat = require('pg-format')
const pgpool = createPool('postgresql://postgres@127.0.0.1:15432/auth')
await pgpool.connect(async pgconn => {
const arrliteral = '{' + pgformat('%I', ['en_US']) + '}'
const selectQuery = sql.unnest([
['123jk1h3jh3', 'Title.', JSON.stringify({prop: 'value'}), arrliteral]
], ['text', 'text', 'jsonb', 'text[]'])
await pgconn.query(sql`insert into project (uuid, title, email_service, supported_locales) select uuid::text, title::text, email_service::jsonb, supported_locales::text[] from ${selectQuery} as t (uuid, title, email_service, supported_locales) returning *`)
})
})()and it will INSERT. Of course I still wonder if there is any other solution to achieve this and if |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
There are integer, text and JSON columns in my table and I'm inserting rows seamlessly. Today, I've added an array column
supported_localesastext[]but am unable to insert items to it.This is the DDL of the table:
This is the test file in which I have simulated the error:
This is the error I receive:
Well, the column is surely the type of
text[]but I couldn't cast the expression['en_US']. Is there anyone to show it to me?Error stack trace:
Update:
I further investigate the issue and it seems like it is because of the parsers in the
node-postgreslibrary. When I debug the query generated byslonikis as the following:Slonik sends these parameters to
node-postgresas stated here:slonik/src/connectionMethods/query.ts
Line 22 in 91efde6
Beta Was this translation helpful? Give feedback.
All reactions