Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

610
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda