【问题标题】:querying for a record and its associations with sequelize查询记录及其与 sequelize 的关联
【发布时间】:2020-01-24 21:39:43
【问题描述】:

给定一个表格 Show 和一个表格 Venue,具有以下关联:

Show.belongsTo(Venue, { foreignKey: { name: 'id_venue', allowNull: false } });
Venue.hasMany(Show);

我正在尝试抢占一个场地及其所有相关节目。

const getSingleVenue = async (req, res) => {
    try {
        const { venueName } = req.params;
        const venue = await Venue.findOne({
            where: {
                name: venueName,
            },
            include: [
                { model: Show }
            ]
        })
        res.send(venue);
    }
    catch(err) {
        console.log(err);
        res.send(400);
    }
}

现在,我遇到了错误Unknown column 'show.venueId' in 'field list'

不知道如何编辑我的关联以提供所需的续集。

【问题讨论】:

    标签: javascript sequelize.js querying


    【解决方案1】:

    问题是这个Venue.hasMany(Show);Y'没有定义外键。

    替换一些代码:

    Show.belongsTo(Venue, { foreignKey: { name: 'id_venue', allowNull: 错误的 } }); Venue.hasMany(Show);

    Show.belongsTo(Venue, { foreignKey: { name: 'id_venue', allowNull: false } });
    Venue.hasMany(Show, { foreignKey: { name: 'id_venue', as: shows } };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      • 2017-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多