Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: primary key conflict in upsert #446

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bbaktaeho
Copy link

@bbaktaeho bbaktaeho commented Jul 10, 2024

You do not need to enter the column 'ON CONFLICT' to process 'DO NOTHING' when the Primary Key CONFLICT occurs.

For example,
if you create a partition table using pg_partman extention in PostgreSQL, the parent table might not have a PK.

create table test_table (id int, tt timestamp) partition by range (tt);
create table test_table_template (LIKE test_table);
alter table test_table_template add constraint test_table_pk primary key (id);

select partman.create_parent('temp.test_table', 'tt', 'native', '1 month', p_template_table := 'temp.test_table_template', p_premake := 2);

select constraint_name, constraint_type from information_schema.table_constraints where table_name = 'test_table';
-- no exists primary key
-- +---------------+---------------+
-- |constraint_name|constraint_type|
-- +---------------+---------------+

Create a parent table as above and inject pk into the partitioning table through the template table.
As a result, there is no PK in the parent table.

-- success
insert into test_table (id, tt) values (1, now());

-- there is no unique or exclusion constraint matching the ON CONFLICT specification
insert into test_table (id, tt)
values (1, now()) on conflict (id) do nothing ;

-- success
insert into test_table (id, tt)
values (1, now()) on conflict do nothing ;

So, it looks better to use diesel on_conflict_do_nothing() when it's PK CONFLICT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant