supabase / postgrest-js

Isomorphic JavaScript client for PostgREST.
https://supabase.com
MIT License
962 stars 128 forks source link

Upsert failing on non-null column, even though value exists in db #515

Open MaximusMcCann opened 5 months ago

MaximusMcCann commented 5 months ago

Bug report

Describe the bug

I want to bulk update, but must use upsert. I use upsert but receive issues on non-null columns when they are not provided, despite the value existing on the record in the db.

To Reproduce

Example table:

CREATE TABLE test_table (
  id text primary key,
  other_column text null,
  name text not null --<--- The problem
);

INSERT INTO test_table (id, other_column, name)
VALUES ('id1',null,'some_name');

Updating a single row (via upsert): I know the record exists and the name column is populated. However if I try to upsert:

await supabase
.from('test_table')
.upsert([
{ id: 'id1', other_column: 'other_column_text' }
])

I receive

{
  code: '23502',
  details: 'Failing row contains (id1, other_column_text, null).',
  hint: null,
  message: 'null value in column "name" of relation "test_table" violates not-null constraint'
}

Expected behavior

The row show be "merged", thus it should just update the two columns I provided, and not default the name column to null, which in this case is not allowed.

evelant commented 1 month ago

I'm seeing very broken behavior with upsert, possibly related. I have a trigger that checks if the incoming NEW.version < OLD.version to reject updates from clients without the latest data. When I use upsert NEW.version is somehow set to the wrong value even though I've verified that the data I pass to upsert is correct! I pass { ...rest_of_record, version: 4 } to upsert and my trigger fails because NEW.version is 1 not 4.