gpt4 book ai didi

javascript - 在嵌套的急切加载 Sequelize 查询中,仅获取一些属性

转载 作者:行者123 更新时间:2023-11-29 09:24:06 25 4
gpt4 key购买 nike

这是我从数据库中获取演出及其相关 field 和乐队的查询。我真的只想知道乐队和 field 的名称。 (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', attribute: ['name']},

您还可以考虑字段别名,如下所示:

{ model: 用户,as: 'bands', 属性: [['name', 'band_name']]},

关于javascript - 在嵌套的急切加载 Sequelize 查询中,仅获取一些属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59898356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com