in postgree, assume I have a tmp table, which is the following schema
tmp_data
id | name | ...
--------------------
null | 'test' | ...
10 | 'other' | ...
and I have initial table data
which has the same schema
id | name | ...
--------------------
1 | '1' | ...
2 | '2' | ...
I want to upsert from initial tmp table into final table, and return all ids, so I was thinking to write this query
insert into data (id, name, ...)
select id, name
from tmp_data
on conflict update
set name = excluded.name
returning id
but the problem is that id is auto generated, so – I want to insert the value if it exists (10) and generate if not (null)
I tried to use select case if column null than default – but seems default cant be used in select clause
Splitting query into 2 is not a case for me, as I am using that data in ef core, and I need to do this in 1 batch – and get ids
Result I am expecting
id | name | ...
--------------------
1 | '1' | ...
2 | '2' | ...
3 | 'test' | ...
10 | 'other' | ...
Are there any options?