인프런 커뮤니티 질문&답변
db 삭제
작성
·
152
0
안녕하세요.
model/profile.js
db.Profile.belongsTo(db.User);
model/user.js
db.User.hasMany(db.Profile);
try {
const userId = req.user.id;
await User.destroy({ where: { id: userId } });
await Profile.destroy({ where: { UserId: userId } });
res.status(200).json('ok');
} catch (error) {
console.error(error);
next(error);
}
위와 같은 요청을 만들어서 회원 탈퇴를 하려고 하는데요.
user만 지워지고 user 가 작성한 profile은 지워지지가 않는데 왜 그런걸까요?
아래 그림 참조하시면 user는 없어져서 UserId 만 null 이 되었고 profile은 안 없어지네요 ㅠㅠ
답변 1
1
제로초(조현영)
지식공유자
User를 지우는 순간 처음 세팅에 의해 Profile의 UserId가 null이됩니다.(on delete set null 옵션) 그러니까 그 다음에 Profile.destory해서 지우려고 해도 대상이 없어지죠.
간단하게는 지우는 것 두 개의 순서를 바꾸면 됩니다. 좀 더 효율적인 방법으로는 on delete cascade 옵션을 쓰시면 됩니다.




