Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
Sequelize: Association HasMany only returns one row in nested query

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!