인프런 커뮤니티 질문&답변
include 질문이요
작성
·
196
0
const fullUserWithoutPassword = await User.findOne({
where: { id: user.id },
attributes: {
// attribute: ['id','nickname','email'] => id,nickname,email만 쓰겠다.
exclude: ["password"],
},
include: [
{
model: Post,
attributes: ["id"],
},
{
model: User,
as: "Followings",
attributes: ["id"],
},
{
model: User,
as: "Followers",
attributes: ["id"],
},
],
});
include의 모델 User는 아래와 같이 user와user의 관계에
의해 mysql에 테이블 follow가 만들어 지는데요.
db.User.belongsToMany(db.User, {
through: "Follow",
as: "Followers",
foreignKey: "FollowingId",
});
db.User.belongsToMany(db.User, {
through: "Follow",
as: "Followings",
foreignKey: "FollowerId",
});
이때
{
model: User,
as: "Followings",
attributes: ["id"],
},
{
model: User,
as: "Followers",
attributes: ["id"],
},
1번째 include 모델 User는 테이블follow의 FollowerId
값으로 해당 user를 찾고
2번째는 FollowingId로 user를 찾아서 객체로 반환 해
주는 건가요? 이쪽부분이 잘 이해가 안되네요..





아 맞는거였군요. 확신없이 적었는데 감사합니다