[英]in nested eager loading sequelize query, fetch only some attributes
這是我從數據庫中獲取節目及其相關場地和樂隊的查詢。 我真的只想得到樂隊和場地的名字。 ( name
是這兩個表中的字段。)不過,下面的代碼正在獲取整個記錄,而不僅僅是我想要的字段。
const getAllShows = async (req, res) => {
try {
const shows = await Show.findAll({
include: [
{ model: User, as: 'bands', through: { attributes: ['name'] } },
{ model: Venue, through: { attributes: ['name'] }}
],
});
res.status(200).send(shows);
}
catch(err) {
res.send(400);
}
}
attributes
放錯了地方——它不屬於through
(順便說一句,根據你的關聯,你甚至可能不需要through
)。
嘗試這樣改變:
{ model: User, as: 'bands', attributes: ['name']},
您也可以考慮字段別名,如下所示:
{ model: User, as: 'bands', attributes: [['name', 'band_name']]},
hth
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.