0

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.

Siren O
  • 59
  • 9

1 Answers1

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",
      });
Anatoly
  • 20,799
  • 3
  • 28
  • 42