使用 where() 和 knex 插入现有行

insert into existing row using where() with knex

我想使用 knex.js 将数据插入到现有行中,但我没有看到它在文档中清楚,尝试使用 Where() 作为下面的代码不起作用。我已经看到有一个名为 knex-filter 的 npm 包可能有助于这样做,但我想应该有一种方法可以用 knex.js 如果有人知道如何进行,我将不胜感激。

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

感谢 scaisEdge 的评论,我意识到我必须使用不同的东西而不是 .insert(),并且它可以使用 .update()。 这是有道理的,因为我不想插入而是修改或更新。

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

我认为你应该使用更新而不是插入

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