What happens with duplicates when inserting multiple rows?

后端 未结 2 1323
别跟我提以往
别跟我提以往 2020-12-11 06:25

I am running a python script that inserts a large amount of data into a Postgres database, I use a single query to perform multiple row inserts:

INSERT INTO          


        
2条回答
  •  情话喂你
    2020-12-11 06:37

    Will it stop the entire query and throw an exception? Yes.

    To avoid that, you can look on the following SO question here, which describes how to avoid Postgres from throwing an error for multiple inserts when some of the inserted keys already exist on the DB.

    You should basically do this:

    INSERT INTO DBtable
            (id, field1)
        SELECT 1, 'value'
        WHERE
            NOT EXISTS (
                SELECT id FROM DBtable WHERE id = 1
    );
    

提交回复
热议问题