Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

300
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!