porsager / porsager/postgres

BUG: inconsistent behaviour inserting multiple records with implicit column names and some properties undefined.

Open
#889 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

Description

When inserting records with some properties undefined, and no transformation config to handle this, the expected result is:

UNDEFINED_VALUE: Undefined values are not allowed.

However, when allowing postgres.js to implicitly derive the inserted records' columns, and the insertion contains a mix of records where some properties are undefined, the order of these records changes the results.

For example, given a postgres connection instance without any transform config for undefined values:

await sql.unsafe(/* SQL */ `
  CREATE TABLE test (
    title VARCHAR(255) NOT NULL,
    foo BOOLEAN NOT NULL DEFAULT FALSE
  );
`);

function insertTest(sql, records) {
  return await sql`
    INSERT INTO test ${sql(records)}
    RETURNING *;
  `;
}

await insertTest([{ title: 'fizz' }, { title: 'buzz', foo: true }]);  ❌ 
// Expected:
//   Error: UNDEFINED_VALUE: Undefined values are not allowed
// Actual:
//   Error [PostgresError]: null value in column "foo" of relation "test" violates not-null constraint

await insertTest([{ title: 'buzz', foo: true }, { title: 'fizz' }]); ✅ 
// Expected & Actual: Error: UNDEFINED_VALUE: Undefined values are not allowed

This is a simplified example that happens to also return a different error, however in more complex scenarios, if the first record has undefined properties, the insert can succeed, but the records' data will be incorrect.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the shown postgres connection, table definition, and sql(records) calls, testing both record orders. Compare the resulting errors and returned or stored data; done means implicit column handling consistently rejects undefined values instead of succeeding with incorrect data.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, postgresql
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.