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

607
Views
Sequelize association not a subclass of Sequelize.model?

I am trying to creat an assistion between my tables like this:

Customers.associate = (models) => {
      Customers.hasMany(models.Addresses, {foreignKey: 'id', as: 'aId', 
        onDelete: "cascade",
      });
  
      Customers.hasMany(models.Orderline, {
        onDelete: "cascade",
      });
    };

I have a customers table where a customer can have many addresses if they wish and many orders. I keep getting this error:

throw new Error(${this.name}.hasMany called with something that's not a subclass of Sequelize.Model); Error: Customers.hasMany called with something that's not a subclass of Sequelize.Model

I have looked online for suggestions but I´m still stuck. Thanks for any help/guidance.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

First you need to check if you register all models BEFORE registering any associations. You can look how to do it in my answer
Second you need to correct the first hasMany association definition because I highly doubt that Addresses has id column as a foreign key column pointing to Customers:

// assuming Addresses has customerId as a foreign key column we need to indicate it:
Customers.hasMany(models.Addresses, {foreignKey: 'customerId', as: 'addresses', 
        onDelete: "cascade",
      });

Maybe it would be a good idea to indicate a foreign key in other hasMany association as well:

Customers.hasMany(models.Orderline, {
        foreignKey: 'customerId',
        onDelete: "cascade",
      });
about 4 years ago · Juan Pablo Isaza Report
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!