pg-mem icon indicating copy to clipboard operation
pg-mem copied to clipboard

"query-failed" event not firing on constraint violations

Open nachovigilante opened this issue 9 months ago • 0 comments

Bug description

Whenever a query incurs in a constraint violation (such as unique or foreign key constraints), the "query-failed" event is not fired. This bug prevents the user from intercepting those type of errors by subscribing to said event.

To Reproduce

If you run the code below, you'll see that the "query-failed" event is not fired at all, but an error message is displayed

import { newDb } from "pg-mem";

const db = newDb();
db.public.none(`create table test(id integer primary key);`);

db.on("query-failed", (query) => {
    console.log("Query failed:", query);
});

db.public.none(`insert into test values (1), (1);`);

Error displayed

path/to/project/node_modules/.pnpm/[email protected]/node_modules/pg-mem/index.js:9781
            throw new interfaces_1.QueryError({
                  ^

QueryError: ERROR: insert into "test" (id) values ($1) returning "id" - duplicate key value violates unique constraint "test_pkey"
DETAIL: Key (id)=(1) already exists.

🐜 This seems to be an execution error, which means that your request syntax seems okay,
    but the resulting statement cannot be executed → Probably not a pg-mem error.

*️⃣ Failed SQL statement: insert into test values (1), (1);;

👉 You can file an issue at https://github.com/oguimbal/pg-mem along with a way to reproduce this error (if you can), and  the stacktrace:


    at BIndex.add (path/to/project/node_modules/.pnpm/[email protected]/node_modules/pg-mem/index.js:9781:19)
    at MemoryTable.indexElt (path/to/project/node_modules/.pnpm/[email protected]/node_modules/pg-mem/index.js:8508:25)
    at MemoryTable.doInsert (path/to/project/node_modules/.pnpm/[email protected]/node_modules/pg-mem/index.js:8380:14)
    at Insert.performMutation (path/to/project/node_modules/.pnpm/[email protected]/node_modules/pg-mem/index.js:9494:44)
    at Insert._doExecuteOnce (path/to/project/node_modules/.pnpm/[email protected]/node_modules/pg-mem/index.js:4553:29)
    at Insert.enumerate (path/to/project/node_modules/.pnpm/[email protected]/node_modules/pg-mem/index.js:4561:29)
    at enumerate.next (<anonymous>)
    at SelectExec.execute (path/to/project/node_modules/.pnpm/[email protected]/node_modules/pg-mem/index.js:3202:41)
    at path/to/project/node_modules/.pnpm/[email protected]/node_modules/pg-mem/index.js:5900:42
    at pushExecutionCtx (path/to/project/node_modules/.pnpm/[email protected]/node_modules/pg-mem/index.js:620:16) {
  data: {
    error: 'insert into "test" (id) values ($1) returning "id" - duplicate key value violates unique constraint "test_pkey"',
    details: 'Key (id)=(1) already exists.',
    code: '23505'
  },
  code: '23505',
  location: { start: 0, end: 0 },
  [Symbol(errorDetailsIncluded)]: true
}

pg-mem version

[email protected]

nachovigilante avatar Jun 27 '25 18:06 nachovigilante