codestates / codestates/Cityrunner

[Sequelize] 갑자기 튀어나온 userId를 못찾는 에러

Open
#52 0 comments 0 reactions 1 assignee Claimed by @seyoonkim48 View on GitHub
Error
Dominant language
JavaScript
Stars
2
Forks
1
PR merge metrics
No merged PRs in 30d

Description

> > ### 어떤 에러인가요?
> > findOne 실행시 하나를 찾아야하는데, 찾을 테이블에 userId가 없는데 없는걸 못찾는다고 에러가 났다.
> > ### 에러 메시지
> > ```js
> > code: 'ER_BAD_FIELD_ERROR',
> > errno: 1054,
> > sqlState: '42S22',
> > sqlMessage: "Unknown column 'userId' in 'field list'",
> > sql: "SELECT `id`, `level`, `distance`, `time`, `max`, `title`,
> > `comment`, `location`, `createdAt`, `updatedAt`, `userId`
> > FROM `posts` AS `post` WHERE `post`.`id` = '1';",
> > ```
> >
> >
> > ### 에러 핸들링 방법
> >
> > * 모델 선언 파일에서 다른 테이블과의 관계설정을 해주는거까진 잘 되었는데,
> > 옵션으로 FK의 이름을 설정해주지 않으면 기본으로 PK가 연결이 되었다. (user 테이블의 Id가 PK여서 userId)
> > * 옵션으로 각 모델에 명시시켜준 뒤 제대로 실행이 되었다..^^...
> >
> > ```js
> > // user Model
> > static associate(models) {
> > models.user.hasMany(models.post, {foreignKey : 'postManager'});
> > }
> >
> > // post Model
> > static associate(models) {
> > models.post.belongTo(models.user, {foreignKey : 'postManager'});
> > }
> > ```
> >
> > ### 에러 핸들링을 위해 참고한 레퍼런스 링크
> > https://sequelize.org/master/class/lib/associations/base.js~Association.html
> > `The foreign key column in Picture will now be called uid instead of the default userId.`

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.