[sequelize]associationナシでincludeしたいとき

投稿者:

のっぴきならねぇ事情でassociationナシでincludeしたいとき

こんなふうに解消した

      await model.findAll({
        include: [
          "table1",
          {
            model: "table2",
            required: true,
            // association objectは好きなやつで
            association: new HasOne(
              model,
              "table2",
              {
                as: "table2",
                foreignKey: "piyo_id",
                sourceKey: "piyo_id",
              }
            ),
          },
        ],
      });

参考

https://stackoverflow.com/questions/47215961/sequelize-js-join-tables-without-associations

Node.js超入門 第3版