sql - Possible to upsert in Postgres on conflict on exactly one of 2 columns? -


i have table has 2 columns unique, , upsert if first column (or both columns) has conflict, nothing if second column has conflict. possible?

create table test (     username    varchar(255) not null unique,     email       varchar(255) not null unique,     status      varchar(127) ); 

the following works check conflicts on email:

insert test(username, email)     values ('test', 'test@test.test')     on conflict(email) update set status='upserted'; 

but i'd (invalid syntax below):

(insert test(username, email)     values ('test', 'test@test.test')     on conflict(email) update set status='upserted')     on conflict nothing; 

yes, can this, requires conditional trickery.

first of all, can have 1 on conflict clause, clause can specify multiple columns constraints defined on them. in case on conflict (username, email). when either or both of columns trigger conflict, conflict_action kicks in.

secondly, conflict_action clause should compare values candidate row insertion (referenced excluded) against current values , take appropriate action. do nothing in practice not possible, can assign old value new row effect same (but update happen). not pretty, this:

insert test(username, email)     values ('test', 'test@test.test')     on conflict(username, email) update          set status = case when username != excluded.username -- email offending                           status                        -- "do nothing"                           else 'upserted'                    -- other action                      end; 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -