댓글을 가져올 때는 id가 1인 댓글만 가져오고, 컬럼도 id 컬럼만 가져오도록 하고 있습니다.

    관계 쿼리 시 조회는 위와 같이 하지만 수정, 생성, 삭제 때는 조금 다른 점이 있습니다.

    const user = await User.findOne({});
    const comment = await Comment.create(); 
    await user.addComment(comment);
    // 또는
    await user.addComment(comment.id);

    여러 개를 추가할 때는 배열로 추가할 수 있습니다.

    const user = await User.findOne({});
    const comment1 = await Comment.create();
    const comment2 = await Comment.create();
    await user.addComment([comment1, comment2]);

    관계 쿼리 메서드의 인수로 추가할 댓글 모델을 넣거나 댓글의 아이디를 넣으면 됩니다. 수정이나 삭제도 마찬가지입니다.

     

    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.