I'm fairly new to Sequelize, but not to SQL. But I'm really struggling with how to correctly get my results from a many to many relationship.
So I need to get all the Sheets with a given vendor_id and a given chemical_id.
Here is my Sequelize that is close but not quite producing the right query.
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;
});
},
And here is the query it is producing:
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'));
Towards the end it says ON Sheet.id = chemicals->chemicals_to_loads.load_id
and I learned I simply need to change that Sheet.id
to Sheet.load_id
and everything works perfect! But how do I get the Sequelize to do that?
Update: Here is some troubleshooting I have done to try and fix it but to no avail. I thought I might need to properly define the foreign key in the Sheets table. So in my index.js under models where I am doing a lot of my associations, I added the foreignKey option to this line:
// Sheets- foreign key in sheets table "belongs to" loads table
db.Sheet.belongsTo(db.Load, { foreignKey: 'load_id' });
Unfortunately, that did not seem to have any effect.
Update 2: I am adding in the 'associate' sections of the Chemical and Load models if that helps at all. Chemicals.js (just the associate section):
Chemicals.associate = (models) => {
Chemicals.belongsToMany(models.Load, {
through: 'chemicals_to_loads',
as: 'loads',
foreignKey: 'chemical_id',
otherKey: 'load_id'
});
};
Chemicals.associate = (models) => {
Chemicals.belongsToMany(models.Vendor, {
through: 'vendors_to_chemicals',
as: 'vendors',
foreignKey: 'chemical_id'
});
Loads.js (just the associate section):
Loads.associate = (models) => {
Loads.belongsToMany(models.Chemical, {
through: 'chemicals_to_loads',
as: 'chemicals',
foreignKey: 'load_id',
otherKey: 'chemical_id'
});
};