Knex.js-如何使用表达式更新字段 [英] Knex.js - How To Update a Field With An Expression

查看:125
本文介绍了Knex.js-如何使用表达式更新字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何让Knex创建以下SQL语句:

How do we get Knex to create the following SQL statement:

UPDATE item SET qtyonhand = qtyonhand + 1 WHERE rowid = 8

我们当前正在使用以下代码:

We're currently using the following code:

knex('item')
    .transacting(trx)
    .update({qtyonhand: 10})
    .where('rowid', 8)

但是,为了使清单应用程序能够在多用户环境中工作,我们需要qtyonhand值与当时数据库中的实际值相加或相减,而不是传递在更新语句已执行.

However, in order for our inventory application to work in a multi-user environment we need the qtyonhand value to add or subtract with what's actually in the database at that moment rather than passing a value that may be stale by the time the update statement is executed.

推荐答案

有2种不同的方式

knex('item').increment('qtyonhand').where('rowid',8)

knex('item').update({
  qtyonhand: knex.raw('?? + 1', ['qtyonhand'])
}).where('rowid',8)

这篇关于Knex.js-如何使用表达式更新字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆