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

303
Visualizações
Sequelize: Special methods don't update the database? - Problem updating instance

I've spent a decent chunk of the day confused about the behaviour of one of my methods that should create an instance of a Model, Company, that has a Contact.

I feel like it should be simple, but it's just not outputting the contact field as I'd expect, even though it is saving it to the database correctly:

await sequelize.transaction(async (t) => {

    const company = await Company.create({ name: req.body.companyName }, { transaction: t });
    const contact = await Contact.create({position: req.body.position}, { transaction: t });
        
    await company.addContact(contact, {transaction: t}); // This method does exist
    await company.save(); // Thought this might help in case `addContact` didn't update the db

    console.log(company.dataValues);   // {id:5, name: 'test'}
    console.log(contact.dataValues);   // {id: 7, position: 'Legend'}

    const test = await Company.findOne({where: { name: req.body.companyName }, transaction: t});
    console.log(test.dataValues);   // {id: 5, name: 'test'}, no contact property
    console.log(company.contacts); // is this not posssible instead?

//...

I was expecting this as the output:

{ id: 5, name: 'test', contacts: [{ id: 7, position: 'legend' }] }

Really can't understand why it's not working. Any help would be really appreciated at this point!

** Associations:

Company.hasMany(Contact);
Contact.belongsTo(Company);
about 4 years ago · Juan Pablo Isaza
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