Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

613
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda