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

139
Views
La actualización de Sequelize One to Many Association no funcionará

Así es como mis dos modelos están conectados:

 DebitInvoice.hasMany(DebitInvoiceProduct, { onDelete: 'CASCADE', onUpdate: 'CASCADE', foreignKey: 'invoice_serial' }); DebitInvoiceProduct.belongsTo(DebitInvoice,{ foreignKey: 'serial' });

Estoy tratando de actualizar DebitInvoice y, al hacerlo, también necesito eliminar las filas anteriores de DebitInvoiceProduct y agregar otras nuevas. Es por eso que estoy usando onUpdate: 'CASCADE' . Estoy intentando con este código:

 const result = await DebitInvoice.update({ invoice_for: 'data', invoice_date: 'data', /* other fields */ DebitInvoiceProducts: productList, },{ where: {serial: 'data'} }, include: [DebitInvoiceProduct] );

Puedo actualizar los campos de DebitInvoice pero no sucede nada en DebitInvoiceProduct

¿Dónde estoy haciendo mal o qué debo hacer para obtener lo que necesito?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

la update no tiene la opción de include , por lo que debe llamar a setDebitInvoiceProducts para cada instancia del modelo DebitInvoice o llamar a destroy y create en DebitInvoiceProducts :

 const invoices = await DebitInvoice.findAll({ where: {serial: 'data'} }); for (const invoice of invoices) { const invoiceProducts = productList.filter(x => <here are conditions to get product list for a certain invoice>) await invoice.setDebitInvoiceProducts(invoiceProducts) }

O

 const invoices = await DebitInvoice.findAll({ where: {serial: 'data'} }); await DebitInvoiceProducts.destroy({ where: { invoiceId: { [Op.in]: invoices.map(x => x.id) } } }) for (const invoice of invoices) { await DebitInvoiceProducts.bulkCreate(productList.map(x => ({ productId: x.id, invoiceId: invoice.id }))) }
over 4 years ago · Santiago Trujillo Report
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!