Reputation: 219
I have two models in my aplication, Delivery and DeliveryProblem. DeliveryProblem has a PK (delivery_id) from Delivery:
static associate(models) {
this.belongsTo(models.Delivery, {
foreignKey: 'delivery_id',
as: 'delivery',
});
}
I need select all Deliveries that have a Delivery Problem. In my Controller, a have the follow method:
async index(req, res) {
const response = await DeliveryProblem.findAll({
order: ['id'],
attributes: ['delivery_id'],
});
// Filter all Deliveries with problem
const ids = [...new Set(response.map((x) => x.delivery_id))];
const deliveries = Delivery.findAll({
where: , // <<< How can I filter ?
order: ['id'],
});
return res.json(deliveries);
}
Upvotes: 0
Views: 144
Reputation: 22758
const Sequelize = require('sequelize')
const Op = Sequelize.Op
...
// Filter all Deliveries with problem
const ids = [...new Set(response.map((x) => x.delivery_id))];
const deliveries = Delivery.findAll({
where: {
id: {
[Op.in]: ids
}
},
order: ['id'],
});
Upvotes: 1