2017-05-18 7 views
1

저는 페이스 북 페이지에서 정보를 얻으려고 postgres (psycopg2)와 python을 사용하고 있습니다. json 객체를 얻고 삽입 쿼리 문자열을 만들고 연결하기 위해 모든 게시물을 반복합니다. 일부 사용자의 경우 다음 오류가 발생했습니다.오류 : 키가 이미 있습니다.

ERROR: A duplicate key value violates the unique constraint "fb_post_pkey" 
DETAIL: Key (id) = (xxx) already exists. 

검색어를 커밋하려면 어떻게해야합니까? json 객체에서 중복 키를 삭제할 수 있습니까?

+0

후 샘플 데이터를 사용하여 처리하는 데이터베이스를 사용하는 것입니다되어 있습니다. –

+1

제약 이름에서'ON CONFLICT DO' 사용 가능 –

답변

2

이는 데이터베이스가 제대로 작동하고 있음을 의미합니다. 테이블이 중복을 허용하지 않도록 구성되었습니다. 어떤 비즈니스 요구 사항에 따라 그것을 처리하는 방법은 두 가지가

try: 
    # sql insert command here 

except IntegrityError: 
    # tell the user here. 

다른 옵션은 정상적으로 이것은 ON CONFLICT 절을

The optional ON CONFLICT clause specifies an alternative action to raising a unique violation or exclusion constraint violation error. For each individual row proposed for insertion, either the insertion proceeds, or, if an arbiter constraint or index specified by conflict_target is violated, the alternative conflict_action is taken. ON CONFLICT DO NOTHING simply avoids inserting a row as its alternative action. ON CONFLICT DO UPDATE updates the existing row that conflicts with the row proposed for insertion as its alternative action.

+0

'CONFLICT ON CONSTRAINT fb_post_pkey DO NOTHING'으로 해결했습니다. –