ERROR: ON CONFLICT DO UPDATE command cannot affect row a second timeHint: Ensure...

2024. 9. 11. 10:43·알쓸신잡

postgres db에 insert 하는 과정에서 다음 오류가 발생하였다.

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.

찾아보니, `ON CONFLICT DO UPDATE` 구문을 사용할 때, 동일한 트랜잭션 내에서 중복된 키 값이 여러 번 발생하는 경우에 발생하는 오류이다. 구체적으로, 한 트랜잭션에서 같은 키를 가진 여러 행을 동시에 삽입하려고 하여 발생하는 것이다.  

예를 들어 아래와 같은 쿼리를 실행할 때,

INSERT INTO score
  (name, math, english)
values
  ('paul', 100, 70),
  ('paul', 100, 50)
ON CONFLICT ON CONSTRAINT score_name_key
DO UPDATE
SET (math, english) = (excluded.math, excluded.english)
where school.english < excluded.english;

score의 name을 key값으로 지정했는데, 동일한 paul이 한 트랜잭션 내에서 삽입되려고하여 업데이트되어 바뀌는 방식이 아니라 오류가 나는 것이다. 

즉 개별 트랜잭션으로 처리하거나, 미리 키값을 확인한다음 진행해줘야 한다.

Reference

 

Upsert error (On Conflict Do Update) pointing to duplicate constrained values

I have a problem with ON CONFLICT DO UPDATE in Postgres 9.5 when I try to use more than one source in the FROM statement. Example of working code: INSERT INTO new.bookmonographs (citavi_id,

stackoverflow.com

 

 

[PostgreSQL] upsert(insert .. conflict on ..) 구문 사용하기

참고 URL : http://www.postgresqltutorial.com/postgresql-upsert/ 'ON CONFLICT' 구문은 PostgreSQL 9.5부터 지원한다. tutorial이 잘 설명되어 있어서 특별히 추가할 내용은 없지만, 내가 삽질했던 실수가 있어서 누군가

sungtae-kim.tistory.com

 

728x90

'알쓸신잡' 카테고리의 다른 글

[pgAdmin error] Your account is locked. Please contact the Administrator.  (0) 2024.10.18
Unable to install extension 'ms-toolsai.jupyter' as it is not compatible with VS Code  (0) 2024.09.27
Crontab+anaconda  (0) 2024.08.13
Gitlab to Github by mirroring  (0) 2024.08.10
CUDA call failed lazily at initialization with error: device >= 0 && device < num_gpus INTERNAL ASSERT FAILED at  (0) 2024.08.05
'알쓸신잡' 카테고리의 다른 글
  • [pgAdmin error] Your account is locked. Please contact the Administrator.
  • Unable to install extension 'ms-toolsai.jupyter' as it is not compatible with VS Code
  • Crontab+anaconda
  • Gitlab to Github by mirroring
창빵맨
창빵맨
  • 창빵맨
    Let's be Developers
    창빵맨
    로그인/로그아웃
  • 전체
    오늘
    어제
    • 분류 전체보기 (471)
      • 알쓸신잡 (79)
      • ML & DL (85)
        • Computer v.. (22)
        • NLP (22)
        • 파이썬 머신러닝 완.. (3)
        • 개념정리 (38)
      • 리눅스 (21)
      • 프로젝트 (29)
        • 산불 발생 예측 (6)
        • 음성비서 (12)
        • pdf 병합 프로그.. (0)
        • 수위 예측 (5)
        • 가짜 뉴스 분류 (5)
        • 전력사용량 예측 (1)
      • 코딩테스트 (217)
        • 프로그래머스[Pyt.. (17)
        • 프로그래머스[Fai.. (3)
        • 백준[Python] (160)
        • 이것이취업을위한코딩.. (18)
        • 파이썬 알고리즘 (19)
      • 데이터분석실습 (25)
        • 데이터 과학 기반의.. (18)
        • 헬로 데이터 과학 (7)
      • 메모장 (0)
      • 잡담 (4)
  • Personal

    GITHUB
    Instagram
  • 공지사항

  • 인기 글

  • 태그

    나동빈
    이코테
    BFS
    DFS
    파이썬
    그리디
    dp
    이분탐색
    이것이취업을위한코딩테스트다
    백준
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3

HOME

HOME

상단으로

티스토리툴바