postgres icon indicating copy to clipboard operation
postgres copied to clipboard

Deferred constraint errors not properly detected

Open arusakov opened this issue 2 months ago • 0 comments

When working with deferred constraints and performing inserts without an explicit transaction, postgres.js fails to properly detect when an error occurs.

// table_name contains DEFERRABLE INITIALLY DEFERRED constraint

// insert *value* object with property which breaks the constraint
await sql`INSERT INTO table_name ${sql(value)}`

// no error, no rejected promise

The error is only properly caught when using an explicit transaction with begin() and performing the insert inside the transaction block.

await sql.begin(async (sql) => {
  await sql`INSERT INTO table_name ${sql(value)}`
})
// rejected promise

arusakov avatar Sep 26 '25 18:09 arusakov