Node.js Sequelize-从多对多选择中没有得到正确的结果

Node.js Sequelize-从多对多选择中没有得到正确的结果,node.js,sequelize.js,Node.js,Sequelize.js,我对续集相当陌生,但对SQL不熟悉。但我真的在为如何正确地从多对多关系中获得结果而挣扎 我的模式: 所以我需要得到所有具有给定供应商id和给定化学品id的表 这是我的续集,虽然很接近,但并没有生成正确的查询 GetVendorFiles: async function(args) { console.log('args: ', args); if (!args.vendor_id) { throw new Erro

我对续集相当陌生,但对SQL不熟悉。但我真的在为如何正确地从多对多关系中获得结果而挣扎

我的模式:

所以我需要得到所有具有给定供应商id和给定化学品id的表

这是我的续集,虽然很接近,但并没有生成正确的查询

GetVendorFiles: async function(args) {
            console.log('args: ', args);
            if (!args.vendor_id) {
                throw new Error('Invalid argument: vendor_id');
            }
            let chemical_id = '';
            let load_id = '';

            if (args.load) {
                // first get the load id
                load_id = await db.Load.findOne({
                    attributes: ['id'],
                    where: { value: args.load }
                }).then((r) => console.log('load id: ', r));
            } else if (args.chemical) {
                // first get the load id
                chemical_id = await db.Chemical.findOne({
                    attributes: ['id'],
                    where: { name: args.chemical }
                }).then((r) => {
                    return r.dataValues.id;
                });
            }

            return await db.Vendor.findOne({
                where: { id: args.vendor_id }
            }).then(async function(vendor) {
                // build the include block conditionally based on whether
                // a chemical or load was passed in
                const model =
                    load_id !== ''
                        ? db.Load
                        : chemical_id !== ''
                        ? db.Load
                        : db.Sheet;
                const includeBlock = [
                    {
                        model: db.Load,
                        required: true,
                        as: 'Load',
                        // where: { id: load_id },
                        include: [
                            {
                                model: db.Chemical,
                                required: true,
                                as: 'chemicals',
                                where: { id: chemical_id }
                            }
                        ]
                    }
                ];
                const files = await db.Sheet.findAll({
                    attributes: ['sheet_name', 'sheet_file_name'],
                    include: includeBlock,
                    model: model,
                    where: {
                        deleted_at: null,
                        vendor_id: vendor.dataValues.id
                    }
                });
                if (files) {
                    return files;
                }
                return null;
            });
        },
下面是它正在生成的查询:

SELECT `Sheet`.`id`, `Sheet`.`sheet_name`, `Sheet`.`sheet_file_name`, `chemicals`.`id` AS `chemicals.id`, `chemicals`.`name` AS `chemicals.name`, `chemicals`.`created_at` AS `chemicals.created_at`, `chemicals`.`updated_at` AS `chemicals.updated_a
t`, `chemicals`.`deleted_at` AS `chemicals.deleted_at`, `chemicals`.`created_at` AS `chemicals.createdAt`, `chemicals`.`updated_at` AS `chemicals.updatedAt`, `chemicals`.`deleted_at` AS `chemicals.deletedAt`, `chemicals->chemicals_to_loads`.`created_at` AS `chemicals
.chemicals_to_loads.createdAt`, `chemicals->chemicals_to_loads`.`updated_at` AS `chemicals.chemicals_to_loads.updatedAt`, `chemicals->chemicals_to_loads`.`load_id` AS `chemicals.chemicals_to_loads.load_id`, `chemicals->chemicals_to_loads`.`chemical_id` AS `chemicals.
chemicals_to_loads.ChemicalId` FROM `sheets` AS `Sheet` INNER JOIN ( `chemicals_to_loads` AS `chemicals->chemicals_to_loads` INNER JOIN `chemicals` AS `chemicals` ON `chemicals`.`id` = `chemicals->chemicals_to_loads`.`chemical_id`) ON `Sheet`.`id` = `chemicals->chemi
cals_to_loads`.`load_id` AND (`chemicals`.`deleted_at` IS NULL AND `chemicals`.`id` = 24) WHERE (`Sheet`.`deleted_at` IS NULL AND (`Sheet`.`deleted_at` IS NULL AND `Sheet`.`vendor_id` = '157'));
最后它在Sheet.id=chemicals->chemicals\u to\u loads.load\u id上写着
,我知道我只需要把
Sheet.id
改成
Sheet.load\u id
,一切都很好!但是我如何让续集做到这一点呢

更新:这里是我为尝试修复它而做的一些故障排除,但没有效果。我想我可能需要在Sheets表中正确定义外键。因此,在我的index.js模型下,我做了很多关联,我在这行中添加了foreignKey选项:

// Sheets- foreign key in sheets table "belongs to" loads table
db.Sheet.belongsTo(db.Load, { foreignKey: 'load_id' });
不幸的是,这似乎没有任何效果

更新2:如果有帮助,我将在化学品和负荷模型的“关联”部分添加。 Chemicals.js(仅在associate部分):

Loads.js(仅关联部分):


M:N您必须在M:N中指定其他键

Product.belongsToMany(Category, {
  through: 'product_categories',
  foreignKey: 'objectId', // replaces `productId`
  otherKey: 'typeId' // replaces `categoryId`
});
Category.belongsToMany(Product, {
  through: 'product_categories',
  foreignKey: 'typeId', // replaces `categoryId`
  otherKey: 'objectId' // replaces `productId`
});

ref

我试过这么做。现在我得到这个错误:
错误:未知属性“load\u id”作为targetKey传递,首先在模型“load”上定义这个属性
load\u id
在我自动创建的联接表
中,它不在加载模型中。是的,如果不清楚,很抱歉。除非我做错了,否则这似乎没有帮助。我已经在我的问题中添加了修改过的关联。或者我需要让Sequelize从一开始就重新创建这些表和关联吗?
    Loads.associate = (models) => {
        Loads.belongsToMany(models.Chemical, {
            through: 'chemicals_to_loads',
            as: 'chemicals',
            foreignKey: 'load_id',
            otherKey: 'chemical_id'
        });
    };
Product.belongsToMany(Category, {
  through: 'product_categories',
  foreignKey: 'objectId', // replaces `productId`
  otherKey: 'typeId' // replaces `categoryId`
});
Category.belongsToMany(Product, {
  through: 'product_categories',
  foreignKey: 'typeId', // replaces `categoryId`
  otherKey: 'objectId' // replaces `productId`
});