궤도

[Sequelize] sequelize-auto 도대체 왜 join(include)이 되지 않는가에 대한 고찰 본문

💻 현생/📃 VIVA

[Sequelize] sequelize-auto 도대체 왜 join(include)이 되지 않는가에 대한 고찰

영이오 2021. 2. 10. 16:38

사건은 이렇게 발생했다.

 

    let pb_candidate = await models.problem.findAll({
        attributes: ['pb_sn'],
        include: [
            {
                model: models.workbook,
                where: {
                    workbook_publisher: 'Gyoyuk'
                }
            }
        ],
        where: {
            pbtype_sn: {
                [Op.in]: type_list
            }
        }
    });

~대충 sequelize로 join을 시도한 코드~

 

그리고 이런 오류메세지를 만났다.

workbook is not associated to problem

 

허어...그럴리가 없는데...

 

models/problem.js

    workbook_sn: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'workbook',
        key: 'workbook_sn'
      }
    },

잘 참조하고 있는데...

 

검색을 하다보니 해당 파일에 hasMany, belongsTo 등으로 관계 설정을 해줘야 한다고 한다.

내가 sequelize-auto를 사용해서 생긴 문제인가? 하지만 이제와서 10개가 넘는 테이블을 다시 설정해줄 수는 없었다.

 

models/init-models.js

  problem.belongsTo(workbook, { foreignKey: "workbook_sn"});
  workbook.hasMany(problem, { foreignKey: "workbook_sn"});

그리고 사실 sequelize-auto를 사용하면 init-models.js 파일에 이렇게 따로 fk관계를 넣어준다.

굳이 추가할 필요가 없다는 말이다.

 

github.com/sequelize/sequelize-auto

 

sequelize/sequelize-auto

Automatically generate bare sequelize models from your database. - sequelize/sequelize-auto

github.com

그래서 sequelize-auto를 만든 개발자의 github에 들어가보았다.

 

나와 같은 문제를 겪은 사람들이 보인다.

 

github.com/sequelize/sequelize-auto/issues/369

 

A is not associated to B · Issue #369 · sequelize/sequelize-auto

Hi, I generated all models by sequelize-auto but when I tried to make an inner join on A and B table I received an error: A is not associated to B I read all related posts and finally below code bl...

github.com

작성자피셜 호러블한 방법이 있다.

 

그리고 저때는 init-models가 생기기 전이라 이제 나는 작동해야하는데...

 

그러다 개발자의 readme에서 이걸 발견했다.

 

var models = require("../models");

난 이렇게 쓰고 있는데...

그래서 init-models를 불러오지 못해 생긴 일인가? 싶어 저거랑 똑같이 바꿔줬다.

 

ReferenceError: sequelize is not defined

계속 검색을 했다. 남들은 잘 참조하는 sequelize가 나는 왜...

 

아니 난 sequelize를 코드가 못찾고 있다니까요...?

 

결국 원하는 답을 찾을 수 없었다.

index.js 파일도 수정해보고 버전도 업데이트해보고 이것저것 다해봤는데 init-models 파일을 가져올 수 없었다.

 

n시간의 삽질 끝에 horrible한 방법을 택하고 말았다.

    //init-models.js에 있는데 왜...
    models.problem.belongsTo(models.workbook, { foreignKey: "workbook_sn" });
    models.workbook.hasMany(models.problem, { foreignKey: "workbook_sn" });

    //오답노트 문제들과 유형이 동일하고 교육청 모의고사인 모든 문제들
    let pb_array = new Array();
    let pb_candidate = await models.problem.findAll({
        attributes: ['pb_sn'],
        include: [
            {
                model: models.workbook,
                where: {
                    workbook_publisher: 'Gyoyuk'
                }
            }
        ],
        where: {
            pbtype_sn: {
                [Op.in]: type_list
            }
        }
    });
    for (var i in pb_candidate) {
        pb_array.push(pb_candidate[i].dataValues.pb_sn);
    }

아직 내가 이걸 해결하기엔 내공이 부족한걸까...

Comments