본문 바로가기
TIL

SQL 에러 errno: 121 "Duplicate key on write or update" , ERROR 1061: Duplicate key name ~~

by 케이리케리 2024. 1. 12.

장바구니 아이템 테이블을 생성 중 만난 에러 errno: 121 "Duplicate key on write or update"

 

해당 스키마에 똑같은 이름의 제약 조건이 있다는 에러이다.

https://stackoverflow.com/questions/20512572/duplicate-key-on-write-or-update

 

Duplicate key on write or update?

Duplicate key on write or update First i will like to acknowledge the fact that, i have been to similar posts, but i couldn't find a way to solve this annoying issue which i face as of present.I am

stackoverflow.com

 

그래서 fk 제약조건 이름의 규칙을 정했다.

"fk_기준 테이블명_참조테이블명_참조키" 형식으로 이름을 변경할 것이다.

cartItems .user_id > users.id : fk_cartItems_users_id

likes.user_id > users.id : fk_likes_users_id

 


 

참조키 이름을 변경하고 apply하니까 다시 만난 에러 ㅎ

ERROR 1061: Duplicate key name ~~~

 

테이블에 제약조건 걸때마다 즉, foreign key를 만들때마다 그 컬럼에 index가 자동으로 추가되면서 제약조건의 이름도 추가되어 발생한 에러이다.

내가 설정하지 않았지만 database에서 자동으로 생성하고 있었던 것임

 

해결방법은 인덱스 이름을 변경하면 된다!

indexes 탭에 들어가서 이름 변경 후 apply하면 끝!