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

302
Vistas
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 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