前提・実現したいこと
sequelizeのORMを使って、下記のSQL文を作成したいのですが、エラーとなり困っています。
どのように記述すべきであるかご助言いただけないでしょうか?
発生している問題・エラーメッセージ
SequelizeDatabaseError: Unknown column 'productId' in 'field list'
SequelizeDatabaseError: Column 'product_id' in field list is ambiguous
該当のソースコード
実行したいSQL文
select a.id as productId, a.product_name, c.src from product a ( select product_id, count(*) as sales_num from order_detail group by product_id order by sales_num desc limit 10 ) b, mst_product_photo c where a.id = b.product_id and b.id = c.product_id order by sales_num desc
■モデルの定義
productテーブル
js
1const Sequelize = require('sequelize'); 2 3module.exports = (sequelize) => { 4 const ProductModel = sequelize.define('product', { 5 id : { type: Sequelize.INTEGER(11).UNSIGNED.ZEROFILL , primaryKey: true, autoIncrement: true }, 6 productName : { field: 'product_name', type: Sequelize.STRING }, 7 }, 8 { 9 freezeTableName: true, 10 timestamps: false 11 }); 12 ProductModel.associate = (Models) => { 13 ProductModel.hasMany(Models.mstProductPhoto, { 14 as: 'photoList', 15 foreignKey: 'productId' 16 }); 17 ProductModel.hasMany(Models.OrderDetailModel, { 18 as: 'orderDetailList', 19 foreignKey: 'productId' 20 }); 21 }; 22 return ProductModel; 23};
order_detailテーブル
js
1const Sequelize = require('sequelize'); 2 3module.exports = (sequelize) => { 4 const OrderDetailModel = sequelize.define('order_detail', { 5 id: { type: Sequelize.INTEGER(11).UNSIGNED.ZEROFILL, primaryKey: true, autoIncrement: true }, 6 orderHeaderId: { field: 'order_header_id', type: Sequelize.INTEGER(11).UNSIGNED.ZEROFILL }, 7 productId: { field: 'product_id', type: Sequelize.INTEGER(11).UNSIGNED.ZEROFILL } 8 }, 9 { 10 freezeTableName: true, 11 timestamps: false 12 }); 13 OrderDetailModel.associate = (Models) => { 14 OrderDetailModel.belongsTo(Models.Product, { 15 foreignKey: 'productId', 16 targetKey: 'id' 17 }); 18 }; 19 return OrderDetailModel; 20};
mst_product_photoテーブル
js
1const Sequelize = require('sequelize'); 2 3module.exports = (sequelize) => { 4 const mstProductPhotoModel = sequelize.define('mst_product_photo', { 5 id : { type: Sequelize.INTEGER(11) , primaryKey: true, autoIncrement: true }, 6 productId : { field: 'product_id', type: Sequelize.INTEGER(11).UNSIGNED.ZEROFILL }, 7 src : { type: Sequelize.STRING } 8 }, 9 { 10 freezeTableName: true, 11 timestamps: false 12 }); 13 mstProductPhotoModel.associate = (Models) => { 14 mstProductPhotoModel.belongsTo(Models.Product, { 15 foreignKey: 'product_id', 16 targetKey: 'id' 17 }); 18 }; 19 return mstProductPhotoModel; 20};
SQL発行部分の処理(抜粋)
js
1await Models.Product.findAll({ 2 attributes: [ 3 ['id', 'productId'], 4 'productName' 5 ], 6 // order_detailテーブル結合 7 include: [{ 8 model: Models.OrderDetailModel, 9 as: 'orderDetailList', 10 required: true, // INNER JOIN 11 attributes: [ 12 'productId', 13 [sequelize.fn('count', sequelize.col('product_id')), 'sales_num'] 14 ], 15 group: ['Models.OrderDetailModel.productId'], 16 raw: true, 17 order: sequelize.literal('sales_num DESC') 18 }, 19 // mst_product_photoテーブル結合 20 { 21 model: Models.mstProductPhoto, 22 as: 'photoList', 23 required: true, // INNER JOIN 24 attributes: [ 25 ['id', 'productPhotoId'], 26 'src' 27 ] 28 }]})
補足情報(FW/ツールのバージョンなど)
"sequelize": "^5.19.5",
よろしくお願いいたします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。