I have these associations:
class PurchaseOrders extends Model{
static config(sequelize){
//Configuration
}
static associate(models){
this.hasMany(models.OrderItems, {
as: 'orderItems',
foreignKey: 'purchaseOrderId'
});
}
}
class OrderItems extends Model{
static config(sequelize){
//Configuration
}
static associate(models){
this.belongsTo(models.PurchaseOrders, {
as: 'purchaseOrder',
sourceKey: 'purchaseOrderId'
});
}
}
OrderItems.init(OrderItemsSchema, OrderItems.config(sequelize));
PurchaseOrders.init(PurchaseOrdersSchema, PurchaseOrders.config(sequelize));
OrderItems.associate(sequelize.models);
PurchaseOrders.associate(sequelize.models);
And I'm trying to do this nested query:
const rta = await models.PurchaseOrders.findAll({
include: [
{
model: models.User,
as: 'buyer'
},
{
model: models.OrderItems,
as: 'orderItems',
required: true
}
]
});
All goes right except that the doesn't return all the order items. Just return the first item and it suppose that is a relation one to many.