-
카테고리
-
세부 분야
풀스택
-
해결 여부
미해결
시퀄라이즈
21.11.14 21:19 작성 조회수 110
0
유저 테이블(User)과 대회 테이블(Contest)이 through: 참가(Participate) 인 n:m 관계 through: 순위권(Win) 인 n:m관계 이렇게 2가지 관계를 가지고 있습니다.
대회에는 타입이 INTEGER인 참가포인트(pPoint), 순위권포인트(wPoint) 라는 칼럼이 있습니다.
이 칼럼들을 관계에 넣지않고 대회테이블에 부여한 것은 추후에 변경 될 수 있기 때문에 이렇게 설계했습니다.
이때 참가자들의 보유 포인트가 많은순으로 참가자 정보와 보유 포인트 합계 정보를 같이 받아오는 것이 목표입니다.
우선 각각의 합들을 받아오기 위해서
router.get('/', async (req, res, next) => {
try {
const userList = await User.findAll({
logging: true,
include: [{
model: Contest,
as: "Participated",
through: { model: Participate, attributes: [] },
attributes: ['pPoint'],
}, {
model: Contest,
as: "Won",
through: { model: Win, attributes: [] },
attributes: ['wPoint'],
}],
attributes: ['name', 'studentid', [fn('sum', col('Participated.pPoint')), 'totalppoint'],
[fn('sum', col('Won.wPoint')), 'totalwpoint']],
});
res.json(userList);
} catch (err) {
console.error(err);
next(err);
}
});
답변을 작성해보세요.
답변 0