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.