Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda