public inbox for [email protected]  
help / color / mirror / Atom feed
Insert on conflict do update fails with duplicate rows - there are no duplicates
2+ messages / 2 participants
[nested] [flat]

* Insert on conflict do update fails with duplicate rows - there are no duplicates
@ 2025-01-27 13:18 Pedro Moraes <[email protected]>
  2025-01-27 15:04 ` Re: Insert on conflict do update fails with duplicate rows - there are no duplicates Laurenz Albe <[email protected]>
  0 siblings, 1 reply; 2+ messages in thread

From: Pedro Moraes @ 2025-01-27 13:18 UTC (permalink / raw)
  To: [email protected]

I managed to replicate the issue with the following data & tables attached
download link.

I am trying to insert from bugtest.temp_on_conflict_test into
bugtest.history, both tables have a primary key on history_id so there
cannot be duplicates

The insert query also uses distinct on (history_id)

INSERT INTO bugtest.history
(account_id,appo_id,account_name,resource_owner_name,resource_owner_id,resource_owner_user_id,resource_id,resource_type,preview,resource_date,history_id)

select distinct on (history_id) * from bugtest.temp_on_conflict_test limit
2
ON CONFLICT (history_id) DO UPDATE SET
account_id = excluded.account_id,
resource_owner_name = excluded.resource_owner_name,
resource_owner_user_id = excluded.resource_owner_user_id,
resource_owner_id = excluded.resource_owner_id,
preview = excluded.preview,
resource_date = excluded.resource_date,
account_name = excluded.account_name
RETURNING *

Reproducible dump:
https://limewire.com/d/2d598eff-f7c9-47d2-80d6-7e522ee1688c#ru2TRVilIqQdqmEjr2-_H4z67PHYe2JZAshDErA8...


^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Insert on conflict do update fails with duplicate rows - there are no duplicates
  2025-01-27 13:18 Insert on conflict do update fails with duplicate rows - there are no duplicates Pedro Moraes <[email protected]>
@ 2025-01-27 15:04 ` Laurenz Albe <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: Laurenz Albe @ 2025-01-27 15:04 UTC (permalink / raw)
  To: Pedro Moraes <[email protected]>; [email protected]

On Mon, 2025-01-27 at 10:18 -0300, Pedro Moraes wrote:
> 
> I managed to replicate the issue with the following data & tables attached download link.
> 
> I am trying to insert from bugtest.temp_on_conflict_test into bugtest.history, both tables have a primary key on history_id so there cannot be duplicates
> 
> The insert query also uses distinct on (history_id)
> 
> INSERT INTO bugtest.history (account_id,appo_id,account_name,resource_owner_name,resource_owner_id,resource_owner_user_id,resource_id,resource_type,preview,resource_date,history_id) 
> select distinct on (history_id) * from bugtest.temp_on_conflict_test limit 2
> ON CONFLICT (history_id) DO UPDATE SET 
> account_id = excluded.account_id,
> resource_owner_name = excluded.resource_owner_name,
> resource_owner_user_id = excluded.resource_owner_user_id,
> resource_owner_id = excluded.resource_owner_id,
> preview = excluded.preview,
> resource_date = excluded.resource_date,
> account_name = excluded.account_name
> RETURNING *
> 
> Reproducible dump: 
> https://limewire.com/d/2d598eff-f7c9-47d2-80d6-7e522ee1688c#ru2TRVilIqQdqmEjr2-_H4z67PHYe2JZAshDErA8...

I get this error message:

ERROR:  ON CONFLICT DO UPDATE command cannot affect row a second time
HINT:  Ensure that no rows proposed for insertion within the same command have duplicate constrained values.

The problem is that you are using "SELECT *", which means that you will get the columns
in the order they were defined in the table.  So you end up inserting
"temp_on_conflict_test.account_name" into "history.history_id", which causes the error,
because all rows in "temp_on_conflict_test" have the same value in that column.

Lessons to learn:
- don't use "SELECT *"
- use more appropriate data types
  In the case at hand, using type "uuid" for "history_id" would have got you a more
  understandable error message.

Yours,
Laurenz Albe






^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2025-01-27 15:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-01-27 13:18 Insert on conflict do update fails with duplicate rows - there are no duplicates Pedro Moraes <[email protected]>
2025-01-27 15:04 ` Laurenz Albe <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox