fix: 크루 가입 신청 승인/거부 후 자동 삭제 및 회원 탈퇴 외래 키 제약 조건 해결#148
Merged
Conversation
leepg038292
approved these changes
Nov 9, 2025
Contributor
leepg038292
left a comment
There was a problem hiding this comment.
외래 키 제약 조건 문제 해결 확인했습니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#️⃣ 연관 이슈
작업 목적
문제 1: 승인/거부된 가입 신청이 계속 남아있음
문제 2: 회원 탈퇴 시 외래 키 제약 조건 에러
MVP 참조 문제
crew_statistics.mvp_user_id → users.user_id
가입 신청 처리자 참조 문제
crew_join_requests.processed_by → users.user_id
→ User 삭제 시 500 에러 발생
📋 변경 사항
// 회원 탈퇴 시 MVP 참조 해제 메서드 추가
@Modifying
@query("UPDATE CrewStatisticsEntity cs SET cs.mvpUser = null, cs.mvpDistance = null " +
"WHERE cs.mvpUser.id = :userId")
int clearMvpByUserId(@Param("userId") Long userId);
// 회원 탈퇴 로직에 MVP 참조 해제 추가
int clearedMvpCount = crewStatisticsRepository.clearMvpByUserId(userId);
// Before
joinRequest.approve(getUserEntity(user.getUserId()), "가입 승인");
joinRequestRepository.saveAndFlush(joinRequest); // APPROVED 상태 보관
CrewMemberEntity newMember = CrewMemberEntity.createMember(crew, joinRequest.getUser());
crewMemberRepository.save(newMember);
// After
CrewMemberEntity newMember = CrewMemberEntity.createMember(crew, joinRequest.getUser());
crewMemberRepository.save(newMember);
joinRequestRepository.delete(joinRequest); // ✂️ 승인 시 삭제
// Before
joinRequest.reject(getUserEntity(user.getUserId()), reason); // REJECTED 상태 보관
// After
joinRequestRepository.delete(joinRequest); // ✂️ 거부 시 삭제
🔍 해결 방법
Before (문제 상황)
가입 신청 → 승인 → APPROVED 상태로 저장⚠️
↓
crew_member 추가
↓
processed_by 참조 유지
↓
처리자 탈퇴 시 외래 키 에러!
After (해결)
가입 신청 → 승인 → join_request 삭제 ✂️
↓
crew_member 추가
↓
참조 없음 ✅
↓
처리자 탈퇴 가능!
🎯 기대 효과
-- Before
SELECT COUNT(*) FROM crew_join_requests; -- 수천 개 (모든 승인/거부 히스토리)
-- After
SELECT COUNT(*) FROM crew_join_requests; -- 수십 개 (PENDING만)
🧪 테스트
빌드 테스트
./gradlew clean build -x test
BUILD SUCCESSFUL in 16s
테스트 시나리오
가입 신청 승인
김철수가 크루 가입 신청
크루장이 승인
✅ crew_member 테이블에 김철수 추가
✅ crew_join_requests에서 신청 삭제됨
가입 신청 거부
김철수가 크루 가입 신청
크루장이 거부
✅ crew_member에 김철수 없음
✅ crew_join_requests에서 신청 삭제됨
MVP였던 사용자 탈퇴
홍길동이 50km 뛰어서 MVP 달성
크루장 위임 후 탈퇴
✅ crew_statistics.mvp_user_id → NULL
✅ User 삭제 성공
승인했던 크루장 탈퇴
홍길동(크루장)이 김철수 가입 승인
join_request 삭제됨 (승인 시)
홍길동이 크루장 위임 후 탈퇴
✅ 참조 없으므로 User 삭제 성공
📊 데이터베이스 영향도
기존 데이터 처리
SQL 쿼리 변경
-- 승인 시
-- Before: UPDATE crew_join_requests SET status='APPROVED', ...
-- After: DELETE FROM crew_join_requests WHERE id=?
-- 거부 시
-- Before: UPDATE crew_join_requests SET status='REJECTED', ...
-- After: DELETE FROM crew_join_requests WHERE id=?
-- 탈퇴 시 (추가)
UPDATE crew_statistics SET mvp_user_id=NULL WHERE mvp_user_id=?
🔗 관련 이슈
API 응답 변경 없음
데이터 변경
📝 추가 고려 사항
히스토리가 필요한 경우 (선택사항)
-- 추후 구현 가능
CREATE TABLE crew_join_history (
id BIGINT PRIMARY KEY,
crew_id BIGINT NOT NULL,
user_id BIGINT NOT NULL,
approved_by_nickname VARCHAR(20), -- FK 아닌 문자열로 저장
action VARCHAR(20), -- APPROVED/REJECTED
approved_at DATETIME,
reason VARCHAR(500)
);
✅ 체크리스트
📸 스크린샷 (선택사항)
승인 전:
crew_join_requests
├── id: 123
├── status: PENDING
├── user_id: 456
└── processed_by: null
승인 후:
crew_join_requests
└── (레코드 삭제됨) ✂️
crew_member
├── id: 789
├── crew_id: 1
└── user_id: 456 ✅ 새로 추가
🚀 배포 전 확인사항