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

121
Views
Deleting multiple rows and then inserting multiple rows using Bookshelf/Knex

I'm new to bookshelf.js and KNEX. Currently working in a controller to remove rows associated with an id and then insert new rows into the same table from a request.

Model:

const model = ModelBase.extend({
tableName: 'table',
idAttribute: 'id',
schema: {
    id: Joi.number().integer(),
    year: Joi.number().integer().when('$creating', required),
    // datafields 
}

Controller code:

const { id, requestBody } = req.swagger.params; // pull out request body
const { newRows } = requestBody;                // pull out newRows object from request

                                                // do stuff
.then(() => {
    db.model.where({ id }).fetchAll()           // then fetch all rows that match id
        .then((rowsWithId) => {
            if (!rowsWithId) {
                throw new NotFound('No rows found with id');
            }
            db.model.where({ id }).destroy();   // remove all rows with id from model
            newRows.forEach((r) => {           
                db.model.create( r );           // iterate through newRoles object and insert new rows into model
            }
            newRows.save({ method: 'insert' });                
        })
        .catch(next);
})

When I run the code I receive a 200 (successful request) but in my console I get a bunch of validation issues from Joi, and this error:

CustomError: No Rows Updated

Can someone tell me what I'm doing wrong here? The rows with the id's are being removed from the database, but no new rows are inserted. I've searched high and low for a solution and haven't found much. Also, the bookshelf documentation hasn't been especially helpful.

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!