sequelize/ID not included in attributes on a join table

flobeeee·2021년 5월 19일
0

시행착오

목록 보기
11/43
post-thumbnail

문제발생

조인테이블에 create를 하고 추가된 레코드의 id 를 가져오려고 했는데, 그것만 쏙빼놓고 다른 값들을 다 가져온다. 환장할노릇.

시도

  1. ID not included in attributes on a join table 참고하여
  • allowNull: false 지워봤으나 효과 X
  1. (긴급)조인테이블의 pk값을 불러오지 못하는 이유? 작성자가 얘기한대로
  • bleongsTo, hasMany 로 연결하니까 효과O

의견

/models/index.js 에

Theme.hasMany(User);
User.belongsTo(Theme);

User_Page.belongsTo(User);
User.hasMany(User_Page);

User_Page.belongsTo(Page);
Page.hasMany(User_Page);

//User.belongsToMany(Page, { through: 'User_Page', foreignKey: 'userId', as: 'Page' });
//Page.belongsToMany(User, { through: 'User_Page', foreignKey: 'pageId', as: 'User' });

주석처리한 내용이 불필요하게 들어가 있었다.
belongsTo, hasMany 로 연결을 해두고
또 belongsToMany 로 연결을 했다.

결론: 중요한건 주석처리처럼 연결하면 조인테이블의 id를 가져오는 것 같다.

profile
기록하는 백엔드 개발자

0개의 댓글