인프런 커뮤니티 질문&답변
복습하고있습니다.
작성
·
206
0
복습하고 있습니다.
static associate(db) {
db.User.hasMany(db.Comment, { foreignKey: 'commenter', sourceKey: 'id' });
}
static associate(db) {
Comment.belongsTo(db.User,{ foreignkey:'commenter' , targetKey:'id' });
}
로 연결이 되었는데
const comments = await Comment.findAll({
include: {
model: User,
where: { id: req.params.id },
},
});
SELECT `Comment`.`id`, `Comment`.`comment`, `Comment`.`created_at`, `Comment`.`commenter`, `Comment`.`UserId`, `User`.`id` AS `User.id`, `User`.`name` AS `User.name`, `User`.`age` AS `User.age`, `User`.`married` AS `User.married`, `User`.`comment` AS `User.comment` FROM `comments` AS `Comment` INNER JOIN `users` AS `User` ON `Comment`.`UserId` = `User`.`id` AND `User`.`id` = '1';
comment.UserId로 가르키고 있는데 이게 commenter이 되야하는거 아닌가요? 이부분은 어디서 수정해야하는지 궁금합니다.
그리고 하나더 궁금한게
삭제나 이런것들 할때 임의로 날려서 남에 게시글 삭제하면 안되니 실무에서 매번 login id랑 글쓴 id랑 체크해서 확인하고 삭제처리되게 하나요? 아니면 더좋은 방법이 있나요?
답변 1
0
hi-ster
질문자
지금 필드에 userid컬럼 없는상태입니다. 저렇게 애러가나서 봐봤더니 commenter로 연결이 되어야하는데 쿼리문이 저렇게 날라가서 애러상태가 되어서 제대로 결과값이 안나옵니다.
두개가 있는상태가 아닙니다.
hi-ster
질문자
static associate(db) {
db.Comment.belongsTo(db.User,{ foreignkey:'commenter' , targetKey:'id' });
}
sql: "SELECT `Comment`.`id`, `Comment`.`comment`, `Comment`.`created_at`, `Comment`.`commenter`, `Comment`.`UserId`, `User`.`id` AS `User.id`, `User`.`name` AS `User.name`, `User`.`age` AS `User.age`, `User`.`married` AS `User.married`, `User`.`comment` AS `User.comment` FROM `comments` AS `Comment` INNER JOIN `users` AS `User` ON `Comment`.`UserId` = `User`.`id` AND `User`.`id` = '1';",
수정해도 같게나오고있습니다.
제로초(조현영)
지식공유자
https://github.com/ZeroCho/nodejs-book/blob/master/ch7/7.6/learn-sequelize/models/index.js
이것도 똑같구요?
hi-ster
질문자
static associate(db) {
db.Comment.belongsTo(db.User,{ foreignkey:'commenter' , targetKey:'id' });
db.Comment.belongsTo(db.User, { foreignKey: 'commenter', targetKey: 'id' });
}
이유를 찾은거같습니다.
foreignkey 쓸때 K대문자로 안하면 인식못하나요? ㄷ.ㄷ;;
아래로 하면 정상작동되고 위에껄로하니까 userId를 잡는거같네요!





1111