Error while inserting a JSON array (if object contains the key "type")
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 8.7k
- Forks
- 374
- Avg merge
- 11d 16h
- Merged PRs (30d)
- 1
Description
Hi, everyone,I encountered a strange phenomenon:
if the array object contains the key "type", an error occurs during insertion.
Reproduction Code
const postgres = require('postgres');
const sql = postgres('postgresql://xxxxxxxxxxxxxx', { max: 17 });
/**
AppPage DDL:
CREATE TABLE "public"."AppPage" (
"id" int4 NOT NULL DEFAULT nextval('"AppPage_id_seq"'::regclass),
"name" text COLLATE "pg_catalog"."default" NOT NULL,
"desc" text COLLATE "pg_catalog"."default",
"nodes" jsonb[] DEFAULT ARRAY[]::jsonb[],
"pageSetId" int4,
CONSTRAINT "AppPage_pkey" PRIMARY KEY ("id")
);
*/
async function main() {
await sql`SELECT 1`;
const title1 = 'test1';
const desc1 = 'desc1';
const nodes1 = [{ foo: 'bar' }];
// ok
await sql`INSERT INTO "AppPage" ("name", "desc", "nodes") VALUES (${title1}, ${desc1}, ${nodes1});`;
const title2 = 'test2';
const desc2 = 'desc2';
const nodes2 = [{ type: 'bar' }];
// If the object includes the type key, it will throw an error
await sql`INSERT INTO "AppPage" ("name", "desc", "nodes") VALUES (${title2}, ${desc2}, ${nodes2});`;
/**
ERROR:
${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\types.js:237
.replace(escapeBackslash, '\\\\')
^
TypeError: Cannot read properties of undefined (reading 'replace')
at arrayEscape (${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\types.js:237:6)
at ${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\types.js:264:15
at Array.map (<anonymous>)
at arraySerializer (${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\types.js:255:19)
at options.serializers.<computed> (${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\connection.js:761:45)
at ${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\connection.js:934:36
at Array.forEach (<anonymous>)
at Bind (${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\connection.js:928:16)
at prepared (${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\connection.js:204:7)
at toBuffer (${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\connection.js:190:15)
at cachedError (${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\query.js:170:23)
at new Query (${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\query.js:36:24)
at sql (${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\index.js:112:11)
at main (${workspace}\postgres.js:30:12)
*/
}
main();
Env
Nodejs: v20.11.1
Postgres.js: 3.4.4
PostgreSQL: 15.3
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with src/types.js at arrayEscape and arraySerializer, using the reproduction with a jsonb[] column and an object containing a type key. Trace how the value reaches the serializer and compare it with the working foo example. Done means both demonstrated inserts complete without the TypeError, with regression coverage added if the repository has a relevant test entry point.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, nodejs, postgresql
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100