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

jhhayashi

I have a table that has two columns that are unique, and would like to upsert if the first column (or both columns) has a conflict, but do nothing if only the second column has a conflict. Is this possible?

CREATE TABLE test (
    username    VARCHAR(255) NOT NULL UNIQUE,
    email       VARCHAR(255) NOT NULL UNIQUE,
    status      VARCHAR(127)
);

The following works to check for conflicts on email:

INSERT INTO test(username, email)
    VALUES ('test', '[email protected]')
    ON CONFLICT(email) DO UPDATE SET status='upserted';

But I'd like to do something like this (invalid syntax below):

(INSERT INTO test(username, email)
    VALUES ('test', '[email protected]')
    ON CONFLICT(email) DO UPDATE SET status='upserted')
    ON CONFLICT DO NOTHING;
Patrick

Yes, you can do this, but it requires some conditional trickery.

First of all, you can have only one ON CONFLICT clause, but this clause can specify multiple columns with constraints defined on them. In your case that would be ON CONFLICT (username, email). When either or both of the columns trigger a conflict, the conflict_action kicks in.

Secondly, the conflict_action clause should compare values from the candidate row for insertion (referenced by EXCLUDED) against current values and take appropriate action. DO NOTHING will in practice not be possible, but you can assign the old value to the new row so the effect is the same (but the update will happen). Not pretty, but it will look somewhat like this:

INSERT INTO test(username, email)
    VALUES ('test', '[email protected]')
    ON CONFLICT(username, email) DO UPDATE 
        SET status = CASE WHEN username != EXCLUDED.username -- only email offending
                          THEN status                        -- so "do nothing"
                          ELSE 'upserted'                    -- some other action
                     END;

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Postgres INSERT ON CONFLICT with JSONB

分類Dev

Postgres 9.5 ON CONFLICT DO SELECT

分類Dev

Postgres upsert using results from select

分類Dev

Is it possible to combine columns and rows of a matrix to become one column in R?

分類Dev

Merge 2 dataframe columns into one by matching date

分類Dev

C align columns w/ printf() w/ exactly 2 spaces after each column

分類Dev

Is it possible to update 2 columns of 2 tables using triggering in oracle?

分類Dev

It is possible to do a SELECT MIN(DATE) searching in 2 or more columns?

分類Dev

Is it possible to execute two functions at EXACTLY the same time

分類Dev

How to merge two columns of a `Dataframe` in Spark into one 2-Tuple?

分類Dev

Flex 2 columns where one is fixed width and the other is width 100%

分類Dev

How to combine results from one column and then split into 2 columns

分類Dev

Concatenate 2 columns in one column "title" on a sharepoint list

分類Dev

2 different on click events conflict?

分類Dev

Regex match URL with exactly one folder deep

分類Dev

How to check if exactly one bit is set in an int?

分類Dev

JSON Schema: Validate that exactly one property is present

分類Dev

How to change the background color for exactly one line?

分類Dev

Postgres INSERT ON CONFLICT DO NOTHING vs SELECT + INSERT query

分類Dev

Postgres INSERT ON CONFLICT DO UPDATE vsINSERTまたはUPDATE

分類Dev

Postgres: process each record of a growing table exactly once

分類Dev

PostgreSQL INSERT ON CONFLICT UPDATE(upsert)はすべての除外値を使用します

分類Dev

PostgreSql Having conflict between group by and needing to add two more columns

分類Dev

NHibernate Mapping - 2 different columns in 2 logical tables in one mapping file and c# class

分類Dev

Is it possible to select this in one query?

分類Dev

postgres 9.5でupsertを正しく行う方法

分類Dev

postgres 9.5でupsertを正しく行う方法

分類Dev

postgres 9.5でupsertを正しく行う方法

分類Dev

Is it possible to create a type or interface which is an Array with exactly "n" entries in TypeScript?

Related 関連記事

  1. 1

    Postgres INSERT ON CONFLICT with JSONB

  2. 2

    Postgres 9.5 ON CONFLICT DO SELECT

  3. 3

    Postgres upsert using results from select

  4. 4

    Is it possible to combine columns and rows of a matrix to become one column in R?

  5. 5

    Merge 2 dataframe columns into one by matching date

  6. 6

    C align columns w/ printf() w/ exactly 2 spaces after each column

  7. 7

    Is it possible to update 2 columns of 2 tables using triggering in oracle?

  8. 8

    It is possible to do a SELECT MIN(DATE) searching in 2 or more columns?

  9. 9

    Is it possible to execute two functions at EXACTLY the same time

  10. 10

    How to merge two columns of a `Dataframe` in Spark into one 2-Tuple?

  11. 11

    Flex 2 columns where one is fixed width and the other is width 100%

  12. 12

    How to combine results from one column and then split into 2 columns

  13. 13

    Concatenate 2 columns in one column "title" on a sharepoint list

  14. 14

    2 different on click events conflict?

  15. 15

    Regex match URL with exactly one folder deep

  16. 16

    How to check if exactly one bit is set in an int?

  17. 17

    JSON Schema: Validate that exactly one property is present

  18. 18

    How to change the background color for exactly one line?

  19. 19

    Postgres INSERT ON CONFLICT DO NOTHING vs SELECT + INSERT query

  20. 20

    Postgres INSERT ON CONFLICT DO UPDATE vsINSERTまたはUPDATE

  21. 21

    Postgres: process each record of a growing table exactly once

  22. 22

    PostgreSQL INSERT ON CONFLICT UPDATE(upsert)はすべての除外値を使用します

  23. 23

    PostgreSql Having conflict between group by and needing to add two more columns

  24. 24

    NHibernate Mapping - 2 different columns in 2 logical tables in one mapping file and c# class

  25. 25

    Is it possible to select this in one query?

  26. 26

    postgres 9.5でupsertを正しく行う方法

  27. 27

    postgres 9.5でupsertを正しく行う方法

  28. 28

    postgres 9.5でupsertを正しく行う方法

  29. 29

    Is it possible to create a type or interface which is an Array with exactly "n" entries in TypeScript?

ホットタグ

アーカイブ