I have a table like so in a Postgres DB -
id dataset_id entity_id county_state_id data
34 31 33413 341 JSOB object
35 31 33413 342 JSOB object
36 31 33413 JSOB object
I want to insert or update this table based on if a record already exists in the table. I have written the following query to do so -
INSERT INTO entity (id, dataset_id, entity_id, county_state_id, data)
SELECT
nextval('id_seq'),
(SELECT id FROM dataset WHERE name = 'Payer'),
e.id,
NULL
jsonb_build_object
('a', a,
'b', b,
'c', c,
)
from
entity e
JOIN payer p
ON p.id = e.id
ON CONFLICT (dataset_id, entity_id, data, county_state_id)
DO NOTHING;
I insert the following input into the table -
id dataset_id entity_id county_state_id data
37 31 33413 JSOB object
I would expect the above SQL query to not update any records because this record already exists in the table. But it does insert a record. I suspect this is happening because NULL <> NULL and I am trying to insert a NULL into the county_state_id column. That is an integer column so I cannot insert an empty string into it, so I do not know how to get Postgres to recognize that the above record already exists in the table.
INSERT0.county_state_idthat is a 0.county_state_idof 0 eg. 'no_county_state_id`.