Home > Software engineering >  Upsert with a missing not null column
Upsert with a missing not null column

Time:08-04

So I am trying to do an upsert on a table that has a name field. This field is set to NOT NULL. When i do the upsert im only really concerned with updating an enabled column. My query comes to :

INSERT INTO products (id,enabled) 
VALUES (7260,false)
ON CONFLICT (id) 
DO UPDATE SET enabled = EXCLUDED.enabled RETURNING id

But im getting an error: ERROR: null value in column "name" violates not-null constraint

The NOT NULL violation comes from the INSERT.

Is it possible to do an upsert like this when a not null column is not in the query?

UPDATE:

From the comments below, something like:

INSERT INTO v2.products (id,name, enabled) 
VALUES (7260,'Tester', true)
ON CONFLICT (id) 
DO UPDATE SET enabled = EXCLUDED.enabled RETURNING id

But that still doesnt work for me. I get no error but the entry in the table is not updating

CodePudding user response:

The not null columns should have a default value, else an error will occur when inserting the row.

Alternatively, specify a not-null value within the insert statement (which could be ignored in the update set)

  • Related