karliatto
karliatto

Reputation: 155

insert into existing row using where() with knex

I want to insert data into an existing row with knex.js but I do not see it is clear in the documentation, trying with Where() as in the code below doesn't work. I've seen that there is a npm package called knex-filter that may help in doing so, but I guess there should be a way to do it with knex.js If anyone knows how to proceed I would be very gratefull.

    knex('pets')
    .where({id : petId})
    .insert({image: file.path})
    .then(function(result) {
        console.log('knexjs works!!');
        })
    .catch(function(error) {
        console.log(error);
    });

Upvotes: 1

Views: 561

Answers (2)

ScaisEdge
ScaisEdge

Reputation: 133410

I think you should use update instead of insert

.where('id', petId)
.update({image :file.path})

Upvotes: 3

karliatto
karliatto

Reputation: 155

Thanks to the comment from scaisEdge I realized I had to use something different instead of .insert(), and it works using .update(). It makes sense because I don't want to insert but modify or update.

     knex('pets')
    .where('id', petId)
    .update({image :file.path})
    .then(function(result) {
        console.log('knexjs works!!');
        })
    .catch(function(error) {
        console.log(error);
    });

Upvotes: -1

Related Questions